********************************************************************************
* Do file for dataset construction (last updated on Feb. 27, 2023)
*
* TITLE  : Does Peacekeeping by Civilians Work? Reducing Armed Violence without Armed Force
* JOURNAL: Japanese Journal of Political Science
* AUTHOR : ASANO, Rui
*
* All datasets were constructed using Stata/MP 15.1 for Mac (64-bit Intel).
********************************************************************************

/*Setting*/
	cls
	version 15.1
	clear all
	*set more off, perm
	*set matsize 11000, perm
	*set scheme s1mono, perm
	cd "[set your directory]"





********************************************************************************
*                                 Monthly data                                 *
********************************************************************************

/*UCDP datasets*/

/*The list of datasets that will be used in the section "UCDP datasets" of this do file:
  - (1) UCDP conflict ID translation format: Sample definition (identifier)
  - (2) UCDP war and peace spells          : Sample definition (spatial and time unit)
  - (3) UCDP peace agreements              : Control variable
  - (4) UCDP actor-conflict dyad list      : Control variables (identifier)
  - (5) UCDP NSV/OSV deaths                : Control variables (incl. limited Syria cases)
  - (6) UCDP conflict battle-related deaths: Dependent variables (incl. limited Syria cases)
  - (x) UCDP actor list                    : For reference (to detect the government actors)
  - (x) UCDP conflict termination          : For reference (to check the cases ending "Actor ceases to exist"; as of the version v.2-2015)
  - (x) Gleditsch and Ward country list    : For reference (to check independence dates)*/



*------------------------------------------------------------------------------*
/*(1) UCDP conflict ID translation format                                     */
*------------------------------------------------------------------------------*
	import delimited using "RawData/01. Conflicts/ID Translation/translate_conf.csv", clear
	rename new_id conflict_id
	save "Data/01. Conflicts/UCDP1_id_translation.dta", replace



*------------------------------------------------------------------------------*
/*(2) UCDP war and peace spells                                               */
*------------------------------------------------------------------------------*
/*(a) Intrastate war list*/
	import excel using "RawData/01. Conflicts/ACD/ucdp-prio-acd-191.xlsx", firstrow clear
	*tab side_a_id if type_of_conflict >= 3
	*tab side_b_id if type_of_conflict >= 3
	/*Only intrastate wars will be contained in the dataset for analysis.
	  To do so, I checked if an interstate war is not coded as an intrastate one in error.
	  Note that actor codes for state governments are < 160 in the UCDP's (new) dataset.*/
	keep if type_of_conflict >= 3

	merge m:1 conflict_id using "Data/01. Conflicts/UCDP1_id_translation.dta"
	*tab conflict_id if _merge == 1 /*The ID list does not include these wars: 14074, 14113, 14129, 14268, 14275, and 14333.*/
	*sort conflict_id year
	*by conflict_id: egen min_year = min(year)
	*tab min_year _merge, m /*The conflicts that are not included in the ID list starts in or after 2016.*/
	*drop min_year
	/*The list of UCDP translation IDs reflects data as of UCDP Armed Conflict Dataset (ACD) version 17.1,
	  which covers time periods between 1946 and 2016. Therefore, some conflicts
	  that started in and after 2017 or UCDP newly added as a war after the version,
	  like in version 19.1 I use here, do not have old conflict IDs.*/
	drop if _merge == 2
	drop _merge

	duplicates drop conflict_id start_date, force /*start_date indicates the first date with at least one battle-related death(s).*/
	keep conflict_id old_id start_date gwno_loc territory_name region
	rename gwno_loc GWccode
	destring, replace
	*count /*217 intrastate wars coded on the UCDP/PRIO ACD version 19.1.*/


/*(b) War-month list since the first death*/
	expand ym(2018,12) - ym(year(start_date), month(start_date)) + 1
	bysort conflict_id: gen x = _n
	gen ym = ym(year(start_date), month(start_date)) if x == 1
	by conflict_id: replace ym = ym[_n-1] + 1 if x != 1
	format ym %tm

	gen month = month(start_date) if x == 1
	count if month == .
	while r(N) > 0 {
		by conflict_id: replace month = month[_n-1] + 1 if x != 1 & month[_n-1] <= 11
		by conflict_id: replace month = 1 if month[_n-1] == 12
		count if month == .
	}

	gen year = year(start_date) if x == 1
	count if year == .
	while r(N) > 0 {
		by conflict_id: replace year = year[_n-1] if x != 1 & month[_n-1] <= 11
		by conflict_id: replace year = year[_n-1] + 1 if month[_n-1] == 12
		count if year == .
	}
	drop x

	order conflict_id old_id ym year month GWccode, first
	sort conflict_id ym
	foreach var of varlist * {
		label variable `var' ""
	}
	save "Data/01. Conflicts/UCDP2_war_and_peace_spells.dta", replace



*------------------------------------------------------------------------------*
/*(3) UCDP peace agreements                                                   */
*------------------------------------------------------------------------------*
	import excel using "RawData/01. Conflicts/Peace Agreement/ucdp-peace-agreements-191.xlsx", sheet("Dataset") firstrow clear
	destring, replace

/*(a) Make variables and re-code values*/
	/*# of dyads*/
	split dyad_id, p(", ") gen(dyad_id_)
	foreach var of varlist dyad_id_* {
		replace `var' = "1" if `var' != ""
		replace `var' = "0" if `var' == ""
		destring `var', replace
	}
	egen num_dyad_pa = rowtotal(dyad_id_*)
	drop dyad_id_*
	*count
	*count if num_dyad_pa == nodyad

	/*Partiality*/
	*tab inclusive, m
	*list if inclusive == -1
	/*Indonesia: Aceh (Points for Further Consultation; Feb. 3, 2002):
	  A variable 'inclusive' should not be coded as -1 because the codebook says
	  that the variable is coded as 1 or 2 (p. 8), and missing values are coded as -99 (p. 10).
	  Another agreement for this conflict in 2002 was concluded between the same parties,
	  that is, the Government of Indonesia and GAM. Thus, I re-code `inclusive' from -1 to 1.*/
	recode inclusive (-1=1)
	rename inclusive exclusive
	recode exclusive (1=0) (2=1)

	/*Agreement type*/
	*tab patype, m
	gen full = 0
	replace full = 1 if patype == 1
	gen process = 0
	replace process = 1 if patype == 3


/*(b) Unit of analysis has been changed to a conflict-agreement:
	  Some peace agreements coded in this dataset are for multiple conflicts.
	  Note that, in contrast, some conflicts are in multiple peace agreements.*/
	split conflict_id, p(", ") gen(conflict_id_)
	foreach num of numlist 1/3 {
		gen x`num' = 1 if conflict_id_`num' != ""
		replace x`num' = 0 if conflict_id_`num' == ""
		destring conflict_id_`num', replace
	}
	egen expand = rowtotal(x*)
	expand expand
	drop conflict_id
	bysort paid: gen conflict_id = _n
	*sum conflict_id_*
	foreach num of numlist 1/3 {
		replace conflict_id = conflict_id_`num' if conflict_id == `num'
	}
	drop conflict_id_* x1-x3 expand


/*(c) Time unit of analysis (time span)*/
	gen pa_start_date = mdy(real(regexs(3)), real(regexs(4)), real(regexs(2))) if regexm(pa_date, "(([0-9]+)[\-]([0-9]+)[\-]([0-9]+))")
	gen pa_end_date = mdy(real(regexs(3)), real(regexs(4)), real(regexs(2))) if regexm(duration, "(([0-9]+)[\-]([0-9]+)[\-]([0-9]+))")
	*count if ended == "True"
	*count if pa_end_date != .
	*br if ended == "True" & pa_end_date == .
	/*Philippines: Mindanao (paid == 1470 / 1471 / 1472 / 1476 / 1478)
	  The variables 'ended' or 'duration' for these agreements may be miscoded.
	  These five agreements were signed between the Government of Philippines and MILF in 2013 or 2014.
	  "In spite of the peace agreement signed in 2014, fighting between the government and MILF
	   erupted in January 2015 shattering the three-year ceasefire"
	   (<https://ucdp.uu.se/additionalinfo/308/1#2014> last access on Feb. 18, 2020).
	  The recurrence date of fighting between the Government of Philippines and MILF
	  was Jan. 25, 2015 according to the UCDP/PRIO ACD Dyadic version 19.1.
	  These statements imply that these peace agreements ended on Jan. 25, 2015,
	  but because I cannot justify this (my/corrected) coding, I regard
	  that these agreements have not ended for a convenience for the analysis.
	  (Note: It seems these cases are typos according to the reply from the author of the dataset.)*/
	replace ended = "False" if ended == "True" & pa_end_date == .
	*count if pa_end_date < pa_start_date
	*list if pa_end_date < pa_start_date
	/*Comoros: Anjouan (paid == 1024)
	  "The agreement was unpopular from the outset, receiving severe criticism
	   from both the internal opposition on the two islands and from the OAU.
	   It was never implemented, and the parties began fresh negotiations
	   under the auspices of OAU and la Francophone in early 2001"
	  (<https://ucdp.uu.se/additionalinfo/407/1#2000> last access on Feb. 18, 2020).*/
	drop if pa_end_date < pa_start_date
	*sum pa_end_date if ended == "True" /*max = 20681 < 21549 = mdy(12,31,2018)*/
	replace pa_end_date = dateintervalendmeta if pa_end_date == .
	format %td pa_start_date pa_end_date
	save "Data/01. Conflicts/UCDP3_peace_agreements_base.dta", replace


/*(d) Panel data of conflict-months*/
	*use "Data/01. Conflicts/UCDP3_peace_agreements_base.dta", clear
	expand ym(year(pa_end_date), month(pa_end_date)) - ym(year(pa_start_date), month(pa_start_date)) + 1
	bysort conflict_id paid: gen x = _n
	gen ym = ym(year(pa_start_date), month(pa_start_date)) if x == 1
	by conflict_id paid: replace ym = ym[_n-1] + 1 if x != 1
	format ym %tm

	gen month = month(pa_start_date) if x == 1
	count if month == .
	while r(N) > 0 {
		by conflict_id paid: replace month = month[_n-1] + 1 if x != 1 & month[_n-1] <= 11
		by conflict_id paid: replace month = 1 if month[_n-1] == 12
		count if month == .
	}

	drop year
	gen year = year(pa_start_date) if x == 1
	count if year == .
	while r(N) > 0 {
		by conflict_id paid: replace year = year[_n-1] if x != 1 & month[_n-1] <= 11
		by conflict_id paid: replace year = year[_n-1] + 1 if month[_n-1] == 12
		count if year == .
	}
	drop x paid region-c_duration intarmy-active_conflict termdur-linktofulltextagreement nodyad-dateintervalendmeta num_dyad_pa

	/*When multiple agreements are coded for a conflict-month:
	  Each variable is re-coded as # of agreements in that case.*/
	sort conflict_id ym
	gen signed = 0
	replace signed = 1 if ym == ym(year(pa_start_date), month(pa_start_date))
	by conflict_id ym: egen sum_signed = sum(signed)
	by conflict_id ym: gen pa_valid = _N
	foreach var of varlist cease-process {
		by conflict_id ym: egen pa_`var' = sum(`var')
	}
	gen pa_nonprocess = pa_valid - pa_process
	duplicates drop conflict_id ym, force
	by conflict_id: gen pa_signed = sum(sum_signed)

	keep conflict_id ym year month pa_valid-pa_signed
	order conflict_id ym year month pa_*, first
	foreach var of varlist * {
		label variable `var' ""
	}
	save "Data/01. Conflicts/UCDP3_peace_agreements.dta", replace
	*sum pa_valid /*Min = 1: At least one agreement is valid in all observations.*/



*------------------------------------------------------------------------------*
/*(4) UCDP actor-conflict dyad list                                           */
*------------------------------------------------------------------------------*
/*
  Non-state violence and one-sided violence which are controlled for is only
   - (a) inflicted by the same rebel group(s) which already became active in a given conflict in a given month, and
   - (b) located in the same country whose government is on a side of a given conflict.
  This measurement excludes the cases where
   - (a) a rebel group inflicted violence in state A, but the group does not have intrastate conflicts against state A's government, or
   - (b) a rebel group inflicted violence, but the group does not have any intrastate conflicts according to the UCDP/PRIO ACD.
  The data check below reveals that
   - (a) some rebel groups have intrastate conflicts against multiple states' governments, and similarly,
   - (b) some rebel groups inflicted these types of violence in multiple locations (i.e., states);
   - thus, some rebel group cannot be identified in a unique location.
*/

/*(a) Data checks*/
	/*Dyadic ACD version 19.1*/
	import excel using "RawData/01. Conflicts/ACD/ucdp-dyadic-191.xlsx", firstrow clear
	keep if type_of_conflict >= 3
	destring side_*_id, replace

	sort side_b_id year conflict_id
	by side_b_id year: gen x = _n
	*tab x, m                             /*At maximum, a rebel group had 16 intrastate wars in a given year.*/
	*tab side_b if x >= 2                 /*See the list below.*/
	sort side_b_id conflict_id year
	by side_b_id conflict_id: gen y = _n
	replace y = 0 if y >= 2
	by side_b_id: egen sum_y = sum(y)
	*tab sum_y, m                         /*At maximum, a rebel group had 21 intrastate wars across years.*/
	*tab side_b if sum_y >= 2			  /*See the list below.*/

	/*Rebel groups which had wars against multiple governments:
	   - AQIM      : Algeria, Mali, Mauritania
	   - Al-Shabaab: Kenya, Somalia
	   - IMU       : Pakistan, Tajikistan, Uzbekistan
	   - JNIM      : Burkina Faso, Mali
	   - Jama'atu Ahlis Sunna Lidda'awati wal-jihad (Boko Haram): Cameroon, Nigeria
	   - MUJAO     : Algeria, Mali
	   - POLISARIO : Mauritania, Morocco
	   - SSDM/A    : South Sudan, Sudan
	   - SSLM/A    : South Sudan, Sudan
	   - IS        : 21 states/civil wars
	  Rebel groups which had wars against multple governments, but the wars transformed into others:
	   - CPI                : Hyderabad, India
	   - Croatian irregulars: Bosnia-Herzegovina, Serbia (Yugoslavia)
	   - Serbian irregulars : Bosnia-Herzegovina, Croatia*/

	/*GED version 19.1*/
	import excel using "RawData/01. Conflicts/GED/ged191.xlsx", firstrow clear
	keep if type_of_violence == 2 | type_of_violence == 3
	expand 2 if type_of_violence == 2

	bysort id: gen x = _n
	gen actor = ""
	replace actor = side_a if type_of_violence == 3
	replace actor = side_a if type_of_violence == 2 & x == 1
	replace actor = side_b if type_of_violence == 2 & x == 2
	sort actor country
	by actor country: gen y = _n
	replace y = 0 if y >= 2
	by actor: egen sum_y = sum(y)
	*tab sum_y, m				  /*At maximum, a rebel group is active in 30 locations.*/
	*tab actor if sum_y >= 2
	*return list				  /*157 rebel groups have been involved in NSV/OSV in multiple locations.*/


/*(b) Make the actor-conflict dyad list*/
	import excel using "RawData/01. Conflicts/ACD/ucdp-dyadic-191.xlsx", firstrow clear
	keep if type_of_conflict >= 3
	destring side_*_id, replace   /*Side B rebel group is unique for each war-year.*/
	duplicates drop dyad_id conflict_id start_date, force
	expand 2

	/*Actor-conflict dyads since the first year of a given (dyad) conflict*/
	bysort conflict_id dyad_id: gen x = _n
	gen     actor    = side_a    if x == 1
	replace actor    = side_b    if x == 2
	gen     actor_id = side_a_id if x == 1
	replace actor_id = side_b_id if x == 2
	bysort conflict_id: egen min_start_date = min(start_date)
	duplicates drop conflict_id actor_id, force
	expand ym(2018,12) - ym(year(min_start_date), month(min_start_date)) + 1
	drop x year

	/*Time variables*/
	bysort conflict_id actor_id: gen x = _n
	gen ym = ym(year(min_start_date), month(min_start_date)) if x == 1
	by conflict_id actor_id: replace ym = ym[_n-1] + 1 if x >= 2
	format ym %tm

	gen month = month(min_start_date) if x == 1
	count if month == .
	while r(N) > 0 {
		by conflict_id actor_id: replace month = month[_n-1] + 1 if x != 1 & month[_n-1] <= 11
		by conflict_id actor_id: replace month = 1 if month[_n-1] == 12
		count if month == .
	}

	gen year = year(min_start_date) if x == 1
	count if year == .
	while r(N) > 0 {
		by conflict_id actor_id: replace year = year[_n-1] if x != 1 & month[_n-1] <= 11
		by conflict_id actor_id: replace year = year[_n-1] + 1 if month[_n-1] == 12
		count if year == .
	}

	rename gwno_loc GWccode
	destring GWccode, replace
	keep location GWccode actor actor_id ym year month conflict_id
	order location GWccode actor actor_id ym year month conflict_id, first
	foreach var of varlist * {
		label variable `var' ""
	}
	save "Data/01. Conflicts/UCDP4_actor_conflict_dyad_list.dta", replace



*------------------------------------------------------------------------------*
/*(5) Non-state violence and one-sided violence                               */
*------------------------------------------------------------------------------*
/*(a) Raw data for the Syrian cases*/
	import excel using "RawData/01. Conflicts/GED/ged_syria.xlsx", firstrow clear
	destring, replace
	save "RawData/01. Conflicts/GED/ged_syria.dta", replace


/*(b) Actor-level event data*/
	import excel using "RawData/01. Conflicts/GED/ged191.xlsx", firstrow clear
	append using "RawData/01. Conflicts/GED/ged_syria.dta"
	keep if type_of_violence == 2 | type_of_violence == 3
	expand 2 if type_of_violence == 2
	bysort id: gen x = _n

	gen actor = ""
	replace actor = side_a if type_of_violence == 3
	replace actor = side_a if type_of_violence == 2 & x == 1
	replace actor = side_b if type_of_violence == 2 & x == 2
	gen actor_id = .
	replace actor_id = side_a_new_id if type_of_violence == 3
	replace actor_id = side_a_new_id if type_of_violence == 2 & x == 1
	replace actor_id = side_b_new_id if type_of_violence == 2 & x == 2
	gen bd = .
	replace bd = deaths_civilians if type_of_violence == 3
	replace bd = deaths_a         if type_of_violence == 2 & x == 1
	replace bd = deaths_b         if type_of_violence == 2 & x == 2
	gen bd_total = deaths_a + deaths_b + deaths_civilians + deaths_unknown
	rename country location
	rename country_id GWccode


/*(c) Actor-level monthly data*/
	*count                                            /*69,233 events in total*/
	*count if  year(date_start) !=  year(date_end)    /*     0 events continue into the next year.*/
	*count if month(date_start) != month(date_end)    /* 2,961 events = 4.3% continue into the next month.*/
	*gen months = month(date_end) - month(date_start)
	*tab months if months >= 1                        /*About 50% of multiple-month events continued over two or three months.*/
	*drop months
	/*When an event does not finish within a certain month, I regard that all the deaths
	  due to the event occur in the last month of the event, because it is
	  impossible to identify exactly in which month each death occurred, and until
	  the last month all the deaths due to the event already occurred.
	  The same specification is applied for conflict battle-related deaths of the dependent variable.*/

	gen ym = ym(year(date_end), month(date_end))
	gen month = month(date_end)
	keep location GWccode ym year month type_of_violence actor-bd_total
	tab type_of_violence, gen(type) /*type1 == NSV; type2 == OSV*/

	sort actor_id GWccode ym /*Unit = actor-location-month*/
	by actor_id GWccode ym: egen num_nsv      = sum(type1)    if type1 == 1
	by actor_id GWccode ym: egen num_osv      = sum(type2)    if type2 == 1
	by actor_id GWccode ym: egen bd_nsv       = sum(bd)       if type1 == 1
	by actor_id GWccode ym: egen bd_osv       = sum(bd)       if type2 == 1
	by actor_id GWccode ym: egen bd_total_nsv = sum(bd_total) if type1 == 1
	by actor_id GWccode ym: egen bd_total_osv = sum(bd_total) if type2 == 1

	foreach var of varlist num_nsv-bd_total_osv {
		by actor_id GWccode ym: egen mean_`var' = mean(`var')
		replace `var' = mean_`var'
		drop mean_`var'
	}
	duplicates drop actor_id GWccode ym, force
	recode num_nsv-bd_total_osv (.=0)

	drop type_of_violence type1 type2 bd bd_total
	order location GWccode actor actor_id ym year month, first
	foreach var of varlist * {
		label variable `var' ""
	}
	save "Data/01. Conflicts/UCDP5_non-state_one-sided_violence_base.dta", replace


/*(d) Merge with the actor-conflict dyad list*/
	use "Data/01. Conflicts/UCDP4_actor_conflict_dyad_list.dta", clear
	merge m:1 actor_id GWccode ym year month using "Data/01. Conflicts/UCDP5_non-state_one-sided_violence_base.dta"
	*sum if _merge == 2
	*tab actor if _merge == 2
	*tab location if _merge == 2
	drop if _merge == 2
	/*_merge == 2: GED includes, but ACD does not include.
	               It indicates that some rebel groups have not inflicted any intrastate wars
				   when they are inflicting non-state violence or one-sided violence.
				   These rebel groups' NSV/OSV are dropped from the dataset.*/
	drop if year <= 1988
	recode num_nsv-bd_total_osv (.=0) if !(location == "Syria" & year <= 2015)
	drop _merge

	sort conflict_id ym actor_id
	by conflict_id ym: gen x = _n
	*count if x == 1
	*count if x == 1 & actor_id <= 160
	*count if x >= 2 & actor_id <= 160 /*All cases are intrastate conflicts.*/
	foreach var of varlist num_nsv-bd_total_osv {
		gen `var'_govt = `var' if x == 1
		by conflict_id ym: egen missing = max(missing(`var'))
		by conflict_id ym: egen `var'_rebel = sum(`var') if x >= 2 & missing == 0
		by conflict_id ym: egen mean_`var'_rebel = mean(`var'_rebel) if missing == 0
		replace `var'_rebel = mean_`var'_rebel if missing == 0
		drop missing mean_`var'_rebel
	}

	keep if x == 1
	drop actor actor_id num_nsv-x *_nsv*govt
	rename *_nsv_rebel *_nsv
	save "Data/01. Conflicts/UCDP5_non-state_one-sided_violence.dta", replace



*------------------------------------------------------------------------------*
/*(6) UCDP conflict battle-related deaths                                     */
*------------------------------------------------------------------------------*
/*(a) Only intrastate conflict events*/
	import excel using "RawData/01. Conflicts/GED/ged191.xlsx", firstrow clear
	append using "RawData/01. Conflicts/GED/ged_syria.dta"
	keep if type_of_violence == 1 /*including interstate conflicts -> dropped below*/

	/*State governments are coded as actor_id < 160, but there are several exceptions according to the UCDP Actor List:
	   - actor_id == 3822: Government of Australia, Government of United Kingdom, Government of United Stated of America;
	   - actor_id == 3912: Government of Malawi;
	   - actor_id == 3986: Government of Zambia;
	   - actor_id == 4499: Government of Solomon Islands; and
	   - actor_id == 6356: Government of United Kingdom, Government of United States of America*/
	*tab conflict_new_id if side_a_new_id < 160 & side_b_new_id < 160 /*Note: 111348 = South Sudan vs Sudan over the common border*/
	drop if ///
		(side_a_new_id <  160 | side_a_new_id == 3822 | side_a_new_id == 3912 |  ///
		side_a_new_id == 3986 | side_a_new_id == 4499 | side_a_new_id == 6356) & ///
		(side_b_new_id <  160 | side_b_new_id == 3822 | side_b_new_id == 3912 |  ///
		side_b_new_id == 3986 | side_b_new_id == 4499 | side_b_new_id == 6356)
	*tab year /*The Syrian version GED includes 2019.*/
	drop if year == 2019

	rename conflict_new_id conflict_id
	merge m:1 conflict_id using "Data/01. Conflicts/UCDP1_id_translation.dta"
	*tab conflict_id if _merge == 1 /*No old IDs for 14074, 14113, 14129, 14268, 14275, 14308, and 14333.*/
	drop if _merge == 2
	drop _merge
	destring old_id, replace


/*(b) Make variables*/
	*count                                            /*116,485 events in total*/
	*count if  year(date_start) !=  year(date_end)    /*      0 events continued into the next year.*/
	*count if month(date_start) != month(date_end)    /*  2,110 events = 1.81% continued into the next month.*/
	*gen months = month(date_end) - month(date_start)
	*tab months if months >= 1                        /*About 75% of multiple-month events continued over only two or three months.*/
	*drop months

	/*Year-month*/
	gen ym = ym(year(date_end), month(date_end))
	gen month = month(date_end)
	sort conflict_id ym id

	/*# of BRDs*/
	foreach var of varlist deaths* low best high {
		by conflict_id ym: egen sum_`var' = sum(`var')
		replace `var' = sum_`var'
		drop sum_`var'
	}
	*count if best == 0                        /*1,384 obs without BRDs at the best estimate*/
	*count if best == 0 & low == 0 & high == 0 /*    0 obs without BRDs at all types of estimates*/

	/*# of events*/
	by conflict_id ym: gen num_event_ged = _N

	/*# of dyads at each event*/
	*egen g1 = group(conflict_id side_a_new_id side_b_new_id)
	*egen g2 = group(conflict_id dyad_new_id)
	*sum g1 g2 /*The same number of groups (375). dyad_new_id should indicate a set of side_a_id and side_b_id.*/
	*drop g1 g2
	duplicates drop conflict_id dyad_new_id ym, force
	by conflict_id ym: gen num_dyad_ged = _N

/*	
	sort conflict_id year dyad_new_id
	egen g = group(conflict_id year dyad_new_id)
	by conflict_id year: egen min_g = min(g)
	by conflict_id year: egen max_g = max(g)
	gen num_dyad_ged_yr = max_g - min_g + 1 /*For the analysis using the annual data if necessary. Note that I do not use this variable for the analysis.*/
	drop g min_g max_g
*/

	duplicates drop conflict_id ym, force
	drop region /*UCDP2 also includes this variable.*/


/*(c) Merge with the base dataset of war and peace spells (= UCDP2)*/
	merge 1:1 conflict_id old_id ym year month using "Data/01. Conflicts/UCDP2_war_and_peace_spells.dta"
	drop if year <= 1988
	*tab conflict_id if _merge == 1
	/*Two intrastate conflicts are coded in the GED, but not in the ACD.
	   - Somalia: Somaliland (conflict_id == 14074)
		 This conflict had the first battle-related death in 2016,
		 but the UCDP/PRIO ACD version 19.1 codes start_date as 2018 and records only after 2018.
		 (Note: This mismatch is due to the difference in the update dates between ACD and GED,
		  according to the reply from the author of the dataset.)
	   - Israel: IS (conflict_id == 14308)
	     This conflict is not recorded in the UCDP ACD version 19.1,
		 probably because 7 (<25) deaths were inflicted only in 2018.*/

	expand 18 if _merge == 1 & conflict_name == "Somalia: Somaliland"   /*Aug. 2016-Dec. 2017*/
	expand  5 if _merge == 1 & conflict_name == "Israel: Islamic State" /*Aug.-Dec. 2018*/
	sort conflict_id ym
	by conflict_id ym: gen x = _n
	replace ym = ym[_n-1] + 1 if _merge == 1 & x != 1
	replace month = month[_n-1] + 1 if _merge == 1 & x != 1
	replace year = year + 1 if month >= 13
	replace month = month - 12 if month >= 13
	replace GWccode = 520 if _merge == 1 & conflict_name == "Somalia: Somaliland"
	replace GWccode = 666 if _merge == 1 & conflict_name == "Israel: Islamic State" /*Note: All the events of this conflict were located in Syria, but side_a is the Government of Israel.*/
	replace start_date = mdy(7,18,2016) if conflict_id == 14074
	replace start_date = mdy(8, 1,2018) if conflict_id == 14308
	*tab conflict_id if GWccode == 652 /*4 conflicts in Syria*/

	foreach var of varlist deaths_a-high num_event_ged num_dyad_ged* {
		replace `var' = . if _merge == 1 & x != 1
	}
	foreach var of varlist deaths_a-high num_event_ged num_dyad_ged {
		recode `var' (.=0) if (_merge == 2 | (_merge == 1 & x != 1)) & !(GWccode == 652 & year <= 2015) /*Conflct-months which are coded in the GED codes, but not in the ACD, nor in Syria before 2015.*/
	}
/*
	bysort conflict_id year: egen mean_num_dyad_ged_yr = mean(num_dyad_ged_yr)
	replace num_dyad_ged_yr = mean_num_dyad_ged_yr if num_dyad_ged_yr == .
	recode num_dyad_ged_yr (.=0) if !(GWccode == 652 & year <= 2015)
*/
	replace territory_name = "Somaliland"    if conflict_id == 14074
	replace territory_name = "Islamic State" if conflict_id == 14308
	replace region = 4 if conflict_id == 14074
	replace region = 2 if conflict_id == 14308
	drop _merge x /*mean_num_dyad_ged_yr*/


/*(d) Merge with other UCDP datasets*/
	/*Peace agreements*/
	merge 1:1 conflict_id ym using "Data/01. Conflicts/UCDP3_peace_agreements.dta"
	*tab year _merge                               /*Only _merge == 2 before 1988*/
	*tab conflict_id if _merge == 2 & year >= 1989 /*Interstate conflicts: conflict_id == 218, 320, 349, 361, 403, 405, 409, 435, and 11348*/
	drop if year <= 1988 | _merge == 2
	drop _merge

	sort conflict_id ym
	recode pa_valid-pa_nonprocess (.=0)
	by conflict_id: replace pa_signed = pa_signed[_n-1] if pa_signed == . /*pa_signed: recode after all peace agreements become broken, until a new agreement is signed.*/
	recode pa_signed (.=0)                                                /*pa_signed: recode before the first peace agreement since 1975.*/

	gen pa = 0
	recode pa (0=1) if pa_valid >= 1 & pa_valid != .

	/*Non-state violence and one-sided violence*/
	merge 1:1 conflict_id ym using "Data/01. Conflicts/UCDP5_non-state_one-sided_violence.dta"
	*tab conflict_id if _merge == 1
	/*These cases are only in the conflict-level ACD, but not in the dyadic-level ACD.
	  start_dates are different in these two datasets.
	   -   396: started on May  15, 1992 (conflict) / Jun.  6, 1993 (dyad)
	   -   399: started on Aug. 20, 1991 (conflict) / Sep. 23, 1993 (dyad)
	   - 11342: started on May  29, 1997 (conflict) / Jun. 27, 2010 (dyad)
	   - 13588: started on Mar. 21, 2015 (conflict) / Jun. 25, 2015 (dyad)
	   - 13721: started on Oct. 10, 2014 (conflict) / Dec. 11, 2014 (dyad)
	  To impute the missing values of the # of events and deaths of non-state/one-sided violence
	  for these conflict-months (i.e., cases where _merge == 1), I search the violence inflicted by the governments
	  and rebel groups of these conflicts on the GED. As a result, the variables are all imputed as 0s.
	  Note: Find such governments (side_a) and rebel groups (side_b) as follows:
	   - tab side_a        if conflict_id == ...
	   - tab side_b_new_id if conflict_id == ...*/
	recode num_nsv-bd_total_osv_rebel (.=0) if !(GWccode == 652 & year <= 2015)
	gsort GWccode -location
	by GWccode: replace location = location[_n-1] if location == ""
	drop _merge


/*(e) "Actor ceases to exist" cases:
	  Some conflicts ended because actor ceases to exist according to UCDP; that is,
	  a state on the side A was dissolved or a disputed incompatibility between A and B changed
	  (and thus rebel groups changed in terms of their organization).
	  However, such conflicts often continued with a new ID of a conflict.
	  I regard that the conflicts before and after actor ceases to exist are
	  the same unique data unit for the analysis. Thus, I re-code variables
	  including ID, location, and others for such cases.
	  
	  Basically, this specification is applied only to territorial disputes,
	  because government disputes against the same state's government can occur
	  even if rebel groups changed. The conflicts over the governments of Somalia and
	  Central African Republic ended because governments were temporarily not observed,
	  but these states still exist as of 2018 and the conflicts over the governments can still recur
	  (Note: these conflicts might not be able to happen while no governments existed,
	  but the exact time span is unclear based on the dataset, codebook, or website).
	  The conflict over the government of Egypt ended because the rebel group
	  joined in IS, but the conflict can still be inflicted by other rebel groups.
	  Exceptionally, Hyderabad was already overthrown by India and has not existed
	  for a long time. Thus, I applied this specification for Hyderabad.
	  
	  -------------------------------------------------------------------------------
	  ceases / incompatibility  	Territory					Government
	  -------------------------------------------------------------------------------
	  Side A changed			USSR: Nagorno-Karabakh		Hyderabad
								Yugoslavia: Croatia			Somalia
															Central African Republic
	  -------------------------------------------------------------------------------
	  Territorial aim changed	Russia: Chechnya			Egypt
								Ukraine: Lugansk/Donetsk
								Syria: Rojava Kurdistan
	  -------------------------------------------------------------------------------
	  
	  See also the UCDP Conflict Termination Dataset v.2-2015 to check the cases:
	   - import excel using "RawData/01. Conflicts/Termination/ucdp-term-conf-2015.xlsx", firstrow clear
	   - list ConflictId Location TerritoryName StartDate EpEndDate if Outcome == 6 & TypeOfConflict >= 3
	  Two conflicts shown by these commands will be dropped from the sample after actor ceases to exist. See (f).
	   -   249 (old_id ==  52): South Vietnam: South Vietnam
	   - 11344 (old_id == 269): Sudan: Abyei
	  Three conflicts' outcomes may be updated after the data publish on the UCDP website (Actor ceases to exist -> Low or no activity).
	   -   222 (old_id ==  24): Myanmar: Government
	   -   426 (old_id == 251): Israel: Southern Lebanon
	   -   440 (old_id == 265): Myanmar: Lahu
	 
	  Some conflicts of "Actor ceases to exist" cannot be found by these commands:
	   - 401 -> 432: UCDP Conflict Termination Dataset Codebook v.2-2015, p.4;
					 "changing territorial aims (such as Russia (Chechnya)
					  becoming Russia (Caucasus Emirate) in 2008" (as an example).
					 The variable 'Outcome' is missing on the dataset.
	   - El Salvador (1981) is also listed as an example of "Actor ceases to exist" in the codebook,
		 but cannot be found in the data or on the website. ACD 19.1 coded this conflict
		 as not ended in 1981, but continued until 1991 and ended by peace agreement
		 (<https://ucdp.uu.se/additionalinfo/316/1#1991conflictarchive> last access on Oct. 26, 2022).
	
	  Lastly, see below details regarding other related cases. Regarding the cases added to UCDP
	  after the publication of the Termination Dataset v.2-2015, I check the website
	  of the UCDP. Ukraine: Lugansk/Donetsk and Syria: Rojava Kurdistan
	  are not coded as "Actor cease to exist" in the Termination Dataset.
	   -   391: "In November 2014, the group pledged allegiance to Islamic State (IS) and became part of it.
				 Since Ansar Bayt al-Maqdis became part of IS, the conflict over government was not seen
				 as active in 2015. For more information see Government of Egypt - IS"
				(<https://ucdp.uu.se/additionalinfo/391/1#2014conflictarchive> last access on Oct. 26, 2022).
				391 ended in 2014, and 13648 over Islamic State started in 2015.
	   -   416: "On 24 March 2013 Seleka seized the capital, Bangui, forcing President Bozizé to flee the country.
				 As a response Anti-Balaka rose up to fight the Seleka government until 1 January 2014
				 interim President Michel Djotodia was forced to resign by international pressure
				 due to the sectarian violence that had spread in the country."
				(<https://ucdp.uu.se/conflict/416/1> last access on Oct. 26, 2022)*/

	/*[1] conflict_id == 376 -> 388 (Nagorno-Karabakh):
		  "Since Azerbaijan is defined as independent from 21 December 1991,
		   the conflict between Nagorno Karabakh and the USSR will be considered terminated from that date"
		  (<https://ucdp.uu.se/additionalinfo/376/1> last access on Oct. 26, 2022),
		  but the conflict over the territory still continues in Azerbaijan.*/
	*sum ym if conflict_id == 388 /*min = 383 = Dec. 1991 = the 1st month*/
	recode conflict_id (376=388)
	recode old_id (181=193)
	replace location = "Azerbaijan" if conflict_id == 388 & ym >= 383
	replace GWccode = 373 if conflict_id == 388 & ym >= 383
	replace territory_name = "Artsakh (Nagorno-Karabakh)" if conflict_id == 388 & ym >= 383

	/*[2] conflict_id == 385 -> 390 (Serb in Croatia):
	      "The violence continued however and to read now between Government of Croatia and Serbian Republic of Krajina"
		  (<https://ucdp.uu.se/additionalinfo/385/1> last access on Oct. 26, 2022).*/
	*sum ym if conflict_id == 390 /*min = 387 = Apr. 1992*/
	recode conflict_id (385=390)
	recode old_id (190=195)
	replace location = "Croatia" if conflict_id == 390 & ym >= 387
	replace GWccode = 344 if conflict_id == 390 & ym >= 387
	replace territory_name = "Serb" if conflict_id == 390 & ym >= 387

	/*[3] conflict_id == 401 -> 432 (Chechnya):
		  "On 7 October 2007, then-President of the Chechen Republic of Ichkeria (ChRI) Doku Umarov proclaimed
		   the Caucasus Emirate, a unified Caucasian Islamic state, and abolished the ChRI. Thus, the goal of
		   achieving independence for Chechnya was subsumed under the broader aim of establishing a Caucasus Emirate.
		   As the incompatibility of the conflicting parties changed, UCDP regards the conflict over Chechnya
		   as terminated by 7 October 2007. Instead, a new conflict over the Caucasus Emirate is recorded
		   as starting the same day" (<https://ucdp.uu.se/additionalinfo/401/1> last access on Oct. 26, 2022).*/
	*sum ym if conflict_id == 432 /*min = 573 = Oct. 2007*/
	recode conflict_id (401=432)
	recode old_id (206=257)
	replace territory_name = "Caucasus Emirate" if conflict_id == 432 & ym >= 573

	/*[4] conflict_id == 11884 -> 227 (Hyderabad):
		  "The conflict reached 25 battle-related deaths in mid-September after the Indian army had entered
		   Hyderabad state and overthrown the government on 13 September. The CPI, which had been involved in
		   a guerilla war against the government of Hyderabad state, subsequently turned against the Indian government"
		  (<https://ucdp.uu.se/additionalinfo/227/1> last access on Oct. 26, 2022)*/
	*sum ym if conflict_id == 227 /*min = 348 = Jan. 1989*/
	recode conflict_id (11884=227)
	recode old_id (19=29)
	replace location = "India" if conflict_id == 227
	replace GWccode = 750 if conflict_id == 227

	/*[5] conflict_id == 13042 -> 13809 (Rojava Kurdistan):
		  "The conflict between the government of Syria and PYD ended as PYD formed the SDF alliance.
		   The conflict continued in 2016 with a dual incompatibility under the name Syria: Government/Rojava Kurdistan"
		  (<https://ucdp.uu.se/additionalinfo/13042/1> last access on Oct. 26, 2022).
		  Incompatibility changed with rebel groups also changed.*/
	recode conflict_id (13042=13809)
	recode old_id (284=13809)

	/*[6] conflict_id == 13246 / 13247 -> 13306 (Novorossiya):
		  "On 16 September 2014, the DPR joined forces with the Lugansk Peoples Republic
		   to form the United Armed Forces of Novorossiya, having a different incompatibility,
		   the whole of Lugansk and Donetsk oblast (a territory referred to them as Novorossiya)"
		  (<https://ucdp.uu.se/additionalinfo/13246/1> last access on Oct. 26, 2022).*/
	*sum ym if conflict_id == 13306 /*min = 656 = Sep. 2014*/
	recode conflict_id (13246=13306) (13247=13306)
	recode old_id (281=283) (282=283)
	replace territory_name = "Donetsk, Lugansk" if conflict_id == 13306 & ym >= 652 & ym <= 655
	replace territory_name = "Novorossiya" if conflict_id == 13306 & ym >= 656

	/*Peace agreements*/
	*tab conflict_id if (conflict_id == 388 | conflict_id == 390 | conflict_id == 432 |      ///
	*					 conflict_id == 227 | conflict_id == 13809 | conflict_id == 13306) & ///
	*                   pa_signed >= 1 /*390 and 13306 signed at least one peace agreements. Thus, they may need to be re-coded.*/
	recode pa_valid pa_full pa_nonprocess pa_signed  (0=1) if conflict_id == 390 & ym >= 430
	recode pa_valid pa_cease pa_nonprocess pa_signed (0=1) if conflict_id == 13306 & ym >= 656 & ym <= 658
	recode pa_signed (0=1) if conflict_id == 13306 & ym >= 659 & ym <= 660
	recode pa_valid pa_cease pa_nonprocess (0=1) if conflict_id == 13306 & ym >= 661
	recode pa_signed (0=2) (1=2) if conflict_id == 13306 & ym >= 661
	recode pa (0=1) if pa_valid >= 1 & pa_valid != .

	/*# of events and deaths
	  Note: When the side A (government) changed for a given conflict, the # of
			events and deaths for the old and new conflicts should be aggregated.
			If not, they should not be aggregated because their #s would be just doubled.
			Regarding the side B (rebel groups), actors changed in all the conflicts.
	  [1]   376 ->   388: (side A) Russia              -> Azerbaijan
						  (side B) Republic of Armenia -> Republic of Artsakh
	  [2]   385 ->   390: (side A) Yugoslavia          -> Croatia
						  (side B) Republic of Croatia / Croatian irregulars -> Serbian Republic of Krajina / Serbian irregulars
	                               Note: Because only Republics were involved in this conflict, the # of dyads is 1 in April 1992.
	  [3]   401 ->   432: (side A) Russia
						  (side B) Chechen Republic of Ichkeria -> Forces of the Caucasus Emirate
	  [4] 11884 ->   227: (side A) India
						  (side B) (? ->) CPI-ML-J / CPI-Maoist / MCC / PWG
								   Note: Because the previous conflict 11884 ended before 1989, and it should have 0 events/deaths after 1989, there's no problems to sum up with 227.
	  [5] 13042 -> 13809: (side A) Syria
						  (side B) (PYD ->) SDF
								   Note: The previous conflict with the rebel group PYD ended in 2015, before which the GED does not code any conflicts for Syria.
	  [6] 1324* -> 13306: (side A) Ukraine
						  (side B) DPR / LPR -> United Armed Forces of Novorossiya*/
	sort conflict_id ym start_date
	foreach var of varlist deaths_a-high num_event_ged num_dyad_ged* num_nsv num_osv_rebel bd_nsv bd_osv_rebel bd_total_nsv bd_total_osv_rebel {
		by conflict_id ym: egen missing = max(missing(`var'))
		by conflict_id ym: egen sum_`var' = sum(`var') if missing == 0
		replace `var' = sum_`var'
		drop missing sum_`var'
	}
	foreach var of varlist num_osv_govt bd_osv_govt bd_total_osv_govt {
		by conflict_id ym: egen missing = max(missing(`var'))
		by conflict_id ym: egen sum_`var' = sum(`var') if missing == 0
		replace `var' = sum_`var' if conflict_id == 388 | conflict_id == 390 /*Side A changed in only these two conflicts. Note that side B changed in all of these conflicts.*/
		drop missing sum_`var'
	}

	foreach var of varlist bd_nsv-bd_total_osv_rebel {
		gen `var'_binary = .
		replace `var'_binary = 0 if `var' == 0
		replace `var'_binary = 1 if `var' >= 1 & `var' != .
	}

	/*# of dyads*/
	recode num_dyad_ged (2=1) if conflict_id == 388 | (conflict_id == 390 & ym == 387) | conflict_id == 432
/*
	recode num_dyad_ged_yr (2=1) if conflict_id == 388 | conflict_id == 432
	recode num_dyad_ged_yr (1=2) (3=2) if conflict_id == 390 & year == 1992
	recode num_dyad_ged_yr (1=3) (2=3) if conflict_id == 13306 & year == 2014
*/

	/*Others*/
	gen territory = 0
	replace territory = 1 if territory_name != ""

	by conflict_id ym: egen min_start_date = min(start_date)
	replace start_date = min_start_date
	drop min_start_date
	duplicates drop conflict_id ym, force


/*(f) Independence cases:
	  After the side A government is dissolved or merged, or the side B group
	  aiming for a territory gained independence (i.e., the disputed territory
	  becomes a state), conflicts over the government or territory cannot occur
	  anymore between the given side A and side B. Thus, these observations are
	  being dropped from the analysis sample. The independence dates are referred
	  in "iisystem.dat" of the Gleditsch and Ward country code list.
	  The UCDP follows their system membership table.*/

	*tab territory_name
	/*Note1: Oman (territory) is a different polity of the current state of Oman.
	  Note2: No conflict-months dropped below had >0 deaths. Search like this:
			  - sum deaths_a-high if conflict_id == ... & ym >= ym(..,..)*/

	/*[1] Taiwan gained independence from China on Dec. 8, 1949.*/
	drop if ym >= ym(1950,1) & conflict_id == 217

	/*[2] Bangladesh gained independence from Pakistan on Dec. 16, 1971, when this episode ended.*/
	drop if conflict_id == 312

	/*[3] Namibia gained independence from South Africa on Mar. 21, 1990.*/
	drop if ym >= ym(1990,4) & conflict_id == 298

	/*[4] Slovenia gained independence from Yugoslavia on Apr. 27, 1992.*/
	drop if ym >= ym(1992,5) & conflict_id == 384

	/*[5] Estonia, Latvia, and Lithuania gained independence from Russia on Sep. 6, 1991.*/
	drop if ym >= ym(1991,10) & conflict_id == 210
	drop if ym >= ym(1991,10) & conflict_id == 211
	drop if ym >= ym(1991,10) & conflict_id == 212

	/*[6] Ukraine gained independence from Russia on Dec. 1, 1991.*/
	drop if ym >= ym(1991,12) & conflict_id == 213

	/*[7] Azerbaijan gained independence from Russia on Dec. 21, 1991.*/
	drop if ym >= ym(1992,1) & conflict_id == 377

	/*[8] Eritrea gained independence from Ethiopia on May 24, 1993.*/
	drop if ym >= ym(1993,6) & conflict_id == 275

	/*[10] East Timor gained independence from Indonesia on May 20, 2002.*/
	drop if ym >= ym(2002,6) & conflict_id == 330

	/*[11] Kosovo gained independence from Serbia on Feb. 17, 2008.*/
	drop if ym >= ym(2008,3) & conflict_id == 412

	/*[12] South Sudan gained independence from Sudan on Jul. 9, 2011.*/
	drop if ym >= ym(2011,8) & conflict_id == 282

	/*[13] Yemen, People's Republic of (South Yemen) was merged into Yemen on May 21, 1990.*/
	drop if ym >= ym(1990,6) & conflict_id == 359

	/*[14] Vietnam, Republic of (South Vietnam) was merged into Vietnam, Democratic Republic of merged on Apr. 30, 1975.*/
	drop if ym >= ym(1975,5) & conflict_id == 249

	/*[15] Sudan and South Sudan have an interstate conflict over Common Border (conflict_id == 11348) after South Sudan gained independence on Jul. 9, 2011.
		   Sudan: Abyei (vs Republic of South Sudan) became South Sudan: Common Border (vs Sudan).
		   This is the only case of "Actor ceases to exist" that changed to an interstate conflict (and is thus being dropped after the independence).*/
	drop if ym >= ym(2011,8) & conflict_id == 11344


/*(g) Cleaning the dataset*/
	rename deaths_a         bd_govt_ged  /*"The best estimate of deaths sustained by side a" (UCDP GED Codebook v19.1, p.11) = deaths_a measures the # of the government side's deaths caused by the rebel side.*/
	rename deaths_b         bd_rebel_ged
	rename deaths_civilians bd_civ_ged
	rename deaths_unknown   bd_unk_ged
	rename low              bd_low_ged
	rename best             bd_best_ged
	rename high             bd_high_ged

	keep conflict_id old_id ym year month GWccode location region territory* ///
		bd_* num_event_ged num_dyad_ged* start_date pa pa_* num_nsv-bd_total_osv_rebel *_binary
	order conflict_id old_id ym year month GWccode location region territory* ///
		bd_best_ged bd_low_ged bd_high_ged bd_govt_ged bd_rebel_ged bd_civ_ged bd_unk_ged ///
		num_event_ged num_dyad_ged* start_date pa pa_* num_nsv-bd_total_osv_rebel *_binary, first

	/*Note that the unit of data is a conflict-month!*/
	label variable conflict_id        "Conflict id"
	label variable old_id             "Old conflict id"
	label variable ym                 "Year-month"
	label variable year               "Year"
	label variable month              "Month"
	label variable GWccode            "Country code"
	label variable location           "Country name"
	label variable region             "Region"
	label variable territory_name     "Territory name"
	label variable territory          "Conflict over territory?"
	label variable bd_best_ged        "# of battle-related deaths (best estimate)"
	label variable bd_low_ged         "# of battle-related deaths (low estimate)"
	label variable bd_high_ged        "# of battle-related deaths (high estimate)"
	label variable bd_govt_ged        "# of battle-related deaths sustained by the govt side"
	label variable bd_rebel_ged       "# of battle-related deaths sustained by the rebel side"
	label variable bd_civ_ged         "# of civilian deaths"
	label variable bd_unk_ged         "# of unknown deaths"
	label variable num_event_ged      "# of war events"
	label variable num_dyad_ged       "# of dyads"
	*label variable num_dyad_ged_yr    "# of dyads (annual)"
	label variable start_date         "When the first death occurred"
	label variable pa                 "Valid peace agreements exist?"
	label variable pa_valid           "# of valid peace agreements"
	label variable pa_cease           "# of valid peace agreements with provisions for a ceasefire"
	label variable pa_exclusive       "# of valid, but not comprehensive, peace agreements"
	label variable pa_full            "# of valid and full peace agreements"
	label variable pa_process         "# of valid peace process agreements"
	label variable pa_nonprocess      "# of valid full/partial peace agreements"
	label variable pa_signed          "# of signed peace agreements since 1975"
	label variable num_nsv            "# of non-state violence events"
	label variable num_osv_govt       "# of govt one-sided violence events"
	label variable num_osv_rebel      "# of rebel one-sided violence events"
	label variable bd_nsv             "# of battle-related deaths of non-state violence"
	label variable bd_osv_govt        "# of civilian deaths of govt one-sided violence"
	label variable bd_osv_rebel       "# of civilian deaths of rebel one-sided violence"
	label variable bd_total_nsv       "# of total deaths of non-state violence"
	label variable bd_total_osv_govt  "# of total deaths of govt one-sided violence"
	label variable bd_total_osv_rebel "# of total deaths of rebel one-sided violence"
	label variable bd_nsv_binary             "Battle-related deaths of non-state violence occur?"
	label variable bd_osv_govt_binary        "Civilian deaths of govt one-sided violence occur?"
	label variable bd_osv_rebel_binary       "Civilian deaths of rebel one-sided violence occur?"
	label variable bd_total_nsv_binary       "Total deaths of non-state violence occur > 0?"
	label variable bd_total_osv_govt_binary  "Total deaths of govt one-sided violence occur > 0?"
	label variable bd_total_osv_rebel_binary "Total deaths of rebel one-sided violence occur > 0?"

	capture label drop region
	label define region 1 "Europe" 2 "Middle East" 3 "Asia" 4 "Africa" 5 "Americas"
	label values region region

	notes conflict_id: "The new version of UCDP conflict id (after version 17.1)"
	notes old_id     : "The old version of UCDP conflict id (before version 17.1)"
	notes GWccode    : "Gleditsch & Ward country codes based on their system membership"
	notes region     : "1=Europe, 2=Middle East, 3=Asia, 4=Africa, 5=Americas"

	save "Data/01. Conflicts/UCDP_datasets.dta", replace





********************************************************************************
********************************************************************************
********************************************************************************

/*Original UN peace operations datasets*/

/*List of datasets that will be constructed and merged in this section:
  - (1) List of UN peace operations  : For preparation
  - (2) Conflict-month data with PKOs: Sample definition
  - (3) Conflict-month data with SPMs: Control variable
  - (4) Adjustment factor 1          : For preparation (surface area size)
  - (5) Adjustment factor 2          : For preparation (population size; not for analysis)
  - (6) PKO financial resources      : Key explanatory variable(s)
  - (7) PKO personnel                : Key explanatory variable(s)
  - (8) All data merged for PKO      : Key explanatory variable(s)*/



*------------------------------------------------------------------------------*
/*(1) List of UN peace operations                                             */
*------------------------------------------------------------------------------*
	import excel using "RawData/02. Peacekeeping/BAPO_v1.xlsx", firstrow clear
	destring, replace
	foreach x in Year Month Day {
		replace Start`x' = Establish`x' if (PrevMission == "" & Start`x' == .) | ///
			MissionAbbrev == "UNAMA" | MissionAbbrev == "UNAMIR" | MissionAbbrev == "UNAMID"
	}
	gen x = mdy(EndMonth, EndDay, EndYear) + 1
	replace StartYear = year(x[_n-1]) if PrevMission != "" & StartYear == .
	replace StartMonth = month(x[_n-1]) if PrevMission != "" & StartMonth == .
	replace StartDay = day(x[_n-1]) if PrevMission != "" & StartDay == .
	keep MissionAbbrev MissionType StartYear-StartDay EndYear-EndDay Ongoing ChapterVII UCDPNewID
	foreach var of varlist * {
		label variable `var' ""
	}
	save "Data/02. Peacekeeping/UNPO1_mission_list.dta", replace /*mission-level data (same as raw data)*/



*------------------------------------------------------------------------------*
/*(2) Conflict-month data with UN PKOs                                         */
*------------------------------------------------------------------------------*
	use "Data/02. Peacekeeping/UNPO1_mission_list.dta", clear
	keep if MissionType == "PKO"
	drop MissionType


/*(a) Monthly data*/
	gen expand = ym(EndYear, EndMonth) - ym(StartYear, StartMonth) + 1 if Ongoing == 0
	replace expand = ym(2019,12) - ym(StartYear, StartMonth) + 1 if Ongoing == 1
	expand expand

	bysort MissionAbbrev: gen x = _n
	gen ym = ym(StartYear, StartMonth) if x == 1
	by MissionAbbrev: replace ym = ym[_n-1] + 1 if x != 1

	gen month = StartMonth if x == 1
	replace month = month[_n-1] + 1 if x != 1
	count if month > 12
	while r(N) > 0 {
		replace month = month - 12 if month > 12
		count if month > 12
	}

	gen year = StartYear if x == 1
	replace year = year[_n-1] if x != 1 & month[_n-1] != 12
	count if year == .
	while r(N) > 0 {
		by MissionAbbrev: replace year = year[_n-1] + 1 if month[_n-1] == 12
		by MissionAbbrev: replace year = year[_n-1] if year == . & month[_n-1] != 12
		count if year == .
	}
	drop expand x StartYear-EndDay Ongoing

	recode ChapterVII (.=0)
	recode ChapterVII (.5=1) if MissionAbbrev == "MONUC" & ym >= ym(2000,3) /*S/RES/1291 covered Mar. 2-Aug. 31, 2000*/
	recode ChapterVII (.5=1) if MissionAbbrev == "UNMIH" & ym >= ym(1994,8) /*S/RES/940 covered Aug. 1, 1994-Jan. 31, 1995*/
	recode ChapterVII (.5=1) if MissionAbbrev == "UNPROFOR" & ym >= ym(1992,8) /*S/RES/770 was published on Aug. 13, 1992*/
	recode ChapterVII (.5=0)


/*(b) Conflict data*/
	*tab MissionAbbrev if UCDPNewID == "" /*MINURCA, ONUC, UNMIT, UNPREDEP, UNSF*/
	drop if UCDPNewID == ""
	split UCDPNewID, p(", ") gen(conflict_id_)
	gen expand = .
	local i = 5
	count if expand == .
	while r(N) > 0 {
		recode expand (.=`i') if conflict_id_`i' != ""
		local i = `i' - 1
		count if expand == .
	}
	expand expand

	bysort MissionAbbrev ym: gen x = _n
	gen conflict_id = .
	foreach num of numlist 1/5 {
		replace conflict_id = real(conflict_id_`num') if x == `num'
	}
	drop UCDPNewID conflict_id_* expand x
	drop if conflict_id == 385 /*conflict_id: 385 is equivalent to 390.*/


/*(c) Conflict-month data (aggregate multiple missions cases)*/
	sort conflict_id ym MissionAbbrev
	by conflict_id ym: egen max_ChapterVII = max(ChapterVII)
	replace ChapterVII = max_ChapterVII
	by conflict_id ym: gen x = _n
	*tab x /*max = 3*/
	gen pko1 = MissionAbbrev if x == 1
	gen pko2 = MissionAbbrev[_n+1] if x == 1 & x[_n+1] == 2
	gen pko3 = MissionAbbrev[_n+2] if x == 1 & x[_n+2] == 3
	drop if x >= 2
	drop MissionAbbrev max_ChapterVII x

	/*just change the order of missions*/
	replace pko2 = "UNIPOM"    if pko1 == "UNIPOM"    & pko2 == "UNMOGIP"
	replace pko1 = "UNMOGIP"   if pko1 == "UNIPOM"    & pko2 == "UNIPOM"
	replace pko2 = "UNAMID"    if pko1 == "UNAMID"    & pko2 == "UNMIS"
	replace pko1 = "UNMIS"     if pko1 == "UNAMID"    & pko2 == "UNAMID"
	replace pko2 = "UNMISET"   if pko1 == "UNMISET"   & pko2 == "UNTAET"
	replace pko1 = "UNTAET"    if pko1 == "UNMISET"   & pko2 == "UNMISET"
	replace pko2 = "UNAMIR"    if pko1 == "UNAMIR"    & pko2 == "UNOMUR"
	replace pko1 = "UNOMUR"    if pko1 == "UNAMIR"    & pko2 == "UNAMIR"
	replace pko2 = "MINUJUSTH" if pko1 == "MINUJUSTH" & pko2 == "MINUSTAH"
	replace pko1 = "MINUSTAH"  if pko1 == "MINUJUSTH" & pko2 == "MINUJUSTH"
	replace pko2 = "UNAMSIL"   if pko1 == "UNAMSIL"   & pko2 == "UNOMSIL"
	replace pko1 = "UNOMSIL"   if pko1 == "UNAMSIL"   & pko2 == "UNAMSIL"
	replace pko2 = "UNMIBH"    if pko1 == "UNMIBH"    & pko2 == "UNPROFOR"
	replace pko1 = "UNPROFOR"  if pko1 == "UNMIBH"    & pko2 == "UNMIBH"
	replace pko2 = "UNCRO"     if pko1 == "UNCRO"     & pko2 == "UNPROFOR"
	replace pko1 = "UNPROFOR"  if pko1 == "UNCRO"     & pko2 == "UNCRO"
	replace pko2 = "UNMOP"     if pko1 == "UNMOP"     & pko2 == "UNTAES"
	replace pko1 = "UNTAES"    if pko1 == "UNMOP"     & pko2 == "UNMOP"
	replace pko3 = "UNPSG"     if pko1 == "UNMOP"     & pko2 == "UNPSG"    & pko3 == "UNTAES"
	replace pko2 = "UNMOP"     if pko1 == "UNMOP"     & pko2 == "UNPSG"    & pko3 == "UNPSG"
	replace pko1 = "UNTAES"    if pko1 == "UNMOP"     & pko2 == "UNMOP"    & pko3 == "UNPSG"

	gen pko = 1
	rename ChapterVII pko_robust
	order conflict_id ym year month pko pko_robust, first
	label variable conflict_id "Conflict id"
	label variable ym          "Year-month"
	label variable year        "Year"
	label variable month       "Month"
	label variable pko         "UN PKO exist?"
	label variable pko_robust  "Robust UN PKO exist?"
	label variable pko1        "UN PKO name no.1"
	label variable pko2        "UN PKO name no.2"
	label variable pko3        "UN PKO name no.3"
	save "Data/02. Peacekeeping/UNPO2_pko_conflict-month.dta", replace


/*Notes on host conflicts of UN PKOs:
  Several conflicts occurred in the host locations are not regarded as the recipients of UN PKOs in my data.
  I referred to The Oxford Handbook of UN PKOs, mission websites, mission maps, and UCDP conflict info to check.
  Most of the cases are the territorial disputes (on a smaller scale) in the host country.

  Apparently UN PKOs were not involved:
   - DRC    : South Kasai    -> occurred only in 1960-62.
   - Sudan  : Southern Sudan -> occurred only in 1963-72.
   - Georgia: South Ossetia/Government -> UNOMIG was deployed for Abkhazia.
   - Lebanon: IS             -> UNIFIL was deployed only to the Southern region of Lebanon, although the war occurred only in the North region of Lebanon.
   - Yugoslavia: Slovenia    -> UNPROFOR was deployed only to Bosnia and Herzegovina, Croatia, and Macedonia.

  For hard test of UN PKO effects:
   - Angola : Cabinda        -> PKO units were deployed, but not the mission's focus.
   - DRC    : Katanga        -> PKO units were deployed, but not the mission's focus.
						        occurred in (1961-62 and) 2013-14, when MONUC-MONUSCO already shifted the center of their activities:
   - DRC    : Kongo Kingdom  -> PKO units were deployed in the Bas Congo province (and Kinshasa), but not the mission's focus.
						        occurred in 2007-08 and 2017, when MONUC-MONUSCO already shifted the center of their activities:
						        "Earlier MONUC operations focused on Ituri and Katanga but by early 2007 the Kivu provinces had become the epicenter of the protection crisis,
						         marked by brutal attacks on civilians by local militias, the CNDP, the armed remnants of the Rwandan exiles (now known as the Democratic Forces
							     for the Liberation of Rwanda—FDLR), and by the government’s own security forces." (The Oxford Handbook, Ch.56)
   - Mali   : Macina Empire  -> PKO units were deployed.
								(MINUSMA responded to FLM attacks as shown in the UNSC reports: S/2015/1030, para.21.)
   - Mali   : IS             -> PKO units were deployed (?)
								(UCDP defines the troop-contributing countries to MINUSMA are on the side A of this conflict.
								 <https://ucdp.uu.se/additionalinfo/14113/1> last access on Nov. 4, 2022)

  Other notes:
   - Sudan: Government hosted UNMIS and UNAMID.
     -> The conflict over the government of Sudan includes the insurgencies in Darfur as well.
		In the UCDP ACD, there is no specific conflict over the Darfur region.*/



*------------------------------------------------------------------------------*
/*(3) Conflict-month data with SPMs                                           */
*------------------------------------------------------------------------------*
	use "Data/02. Peacekeeping/UNPO1_mission_list.dta", clear
	keep if MissionType == "SPM"
	drop MissionType


/*(a) Monthly data*/
	gen expand = ym(EndYear, EndMonth) - ym(StartYear, StartMonth) + 1 if Ongoing == 0
	replace expand = ym(2019,12) - ym(StartYear, StartMonth) + 1 if Ongoing == 1
	drop if expand == .
	expand expand

	bysort MissionAbbrev: gen x = _n
	gen ym = ym(StartYear, StartMonth) if x == 1
	by MissionAbbrev: replace ym = ym[_n-1] + 1 if x != 1

	gen month = StartMonth if x == 1
	replace month = month[_n-1] + 1 if x != 1
	count if month > 12
	while r(N) > 0 {
		replace month = month - 12 if month > 12
		count if month > 12
	}

	gen year = StartYear if x == 1
	replace year = year[_n-1] if x != 1 & month[_n-1] != 12
	count if year == .
	while r(N) > 0 {
		by MissionAbbrev: replace year = year[_n-1] + 1 if month[_n-1] == 12
		by MissionAbbrev: replace year = year[_n-1] if year == . & month[_n-1] != 12
		count if year == .
	}
	drop expand x StartYear-EndDay Ongoing

	recode ChapterVII (.=0)


/*(b) Conflict data*/
	*tab MissionAbbrev if UCDPNewID == "" /*UNOAU, UNOCA, UNOWA, UNOWAS, UNRCCA, UNSCO*/
	drop if UCDPNewID == ""
	split UCDPNewID, p(", ") gen(conflict_id_)
	gen expand = .
	local i = 2
	count if expand == .
	while r(N) > 0 {
		recode expand (.=`i') if conflict_id_`i' != ""
		local i = `i' - 1
		count if expand == .
	}
	expand expand

	bysort MissionAbbrev ym: gen x = _n
	gen conflict_id = .
	foreach num of numlist 1/2 {
		replace conflict_id = real(conflict_id_`num') if x == `num'
	}
	drop UCDPNewID conflict_id_* expand x


/*(c) Conflict-month data (aggregate multiple missions cases)*/
	sort conflict_id ym MissionAbbrev
	by conflict_id ym: egen max_ChapterVII = max(ChapterVII)
	replace ChapterVII = max_ChapterVII
	by conflict_id ym: gen x = _n
	*tab x /*max = 2*/
	gen spm1 = MissionAbbrev if x == 1
	gen spm2 = MissionAbbrev[_n+1] if x == 1 & x[_n+1] == 2
	drop if x >= 2
	drop MissionAbbrev max_ChapterVII x

	/*just change the order of missions*/
	replace spm2 = "UNMA"    if spm1 == "UNMA"  & spm2 == "UNOA"
	replace spm1 = "UNOA"    if spm1 == "UNMA"  & spm2 == "UNMA"
	replace spm2 = "MICAH"   if spm1 == "MICAH" & spm2 == "MICIVIH"
	replace spm1 = "MICIVIH" if spm1 == "MICAH" & spm2 == "MICAH"

	gen spm = 1
	rename ChapterVII spm_robust
	order conflict_id ym year month spm spm_robust, first
	label variable conflict_id "Conflict id"
	label variable ym          "Year-month"
	label variable year        "Year"
	label variable month       "Month"
	label variable spm         "UN SPM exist?"
	label variable spm_robust  "Robust UN SPM exist?"
	label variable spm1        "UN SPM name no.1"
	label variable spm2        "UN SPM name no.2"
	save "Data/02. Peacekeeping/UNPO3_spm_conflict-month.dta", replace



*------------------------------------------------------------------------------*
/*(4) Adjustment factor 1: surface area                                       */
*------------------------------------------------------------------------------*
/*(a) Only UN PKO host countries*/
	import excel using "RawData/03. Controls/World Bank/API_AG.SRF.TOTL.K2_DS2_en_excel_v2_723239.xls", sheet("Data") clear
	local i = 1960
	foreach var of varlist E-BL {
		rename `var' srf`i'
		local i = `i' + 1
	}
	rename A location
	drop if _n <= 4
	drop B-D
	reshape long srf, i(location) j(year)
	destring, replace

	*tab location /*keep only countries that have hosted UN PKOs*/
	keep if ///
		location == "Angola"                   | location == "Bosnia and Herzegovina" | ///
		location == "Burundi"                  | location == "Cambodia"               | ///
		location == "Central African Republic" | location == "Chad"                   | ///
		location == "Congo, Dem. Rep."         | location == "Congo, Rep."            | ///
		location == "Cote d'Ivoire"            | location == "Costa Rica"             | ///
		location == "Croatia"                  | location == "Cyprus"                 | ///
		location == "Dominican Republic"       | location == "El Salvador"            | ///
		location == "Georgia"                  | location == "Guatemala"              | ///
		location == "Haiti"                    | location == "Honduras"               | ///
		location == "Kosovo"                   | location == "Lebanon"                | ///
		location == "Liberia"                  | location == "Mali"                   | ///
		location == "Mozambique"               | location == "Namibia"                | ///
		location == "Nicaragua"                | location == "North Macedonia"        | ///
		location == "Rwanda"                   | location == "Sierra Leone"           | ///
		location == "Somalia"                  | location == "South Africa"           | ///
		location == "South Sudan"              | location == "Sudan"                  | ///
		location == "Syrian Arab Republic"     | location == "Tajikistan"             | ///
		location == "Timor-Leste"              | location == "Yemen, Rep."
	*line srf year, by(location) /*No time variations by country*/
	*tab location if srf == .    /*Sudan and South Sudan have no data for all the periods.*/
	drop if srf == . & !((location == "Sudan" & year == 1960) | (location == "South Sudan" & year == 1960)) /*to make unique data*/
	duplicates drop location, force
	drop year


/*(b) Missing data (Sudan and South Sudan)*/
	replace srf = 1861484 + 644329 if location == "Sudan"
	/*Because Sudan (not Darfur region) hosted UNMIS only before the independence of South Sudan,
	  I use the aggregated surface area of the current (North) Sudan and South Sudan.
	  The area size is 2,505,813 sq. km according to the CIA Factobook. North Sudan = 2505813 - 644329 = 1861484.
	  CIA Factbook <https://web.archive.org/web/20110514005703/https://www.cia.gov/library/publications/the-world-factbook/geos/su.html> last access on Oct. 27, 2022.*/

	replace srf =           644329 if location == "South Sudan"
	/*CIA Factbook <https://web.archive.org/web/20200831043316/https://www.cia.gov/library/publications/the-world-factbook/geos/od.html> last access on Oct. 27, 2022.*/


/*(c) Multi-country regions*/
	/*ONUCA: Central America*/
	*count
	set obs 37
	replace location = "Central America" if location == ""
	sum srf if location == "Costa Rica"
	scalar srf_cos = r(mean)
	sum srf if location == "El Salvador"
	scalar srf_els = r(mean)
	sum srf if location == "Guatemala"
	scalar srf_gua = r(mean)
	sum srf if location == "Honduras"
	scalar srf_hon = r(mean)
	sum srf if location == "Nicaragua"
	scalar srf_nic = r(mean)
	replace srf = srf_cos + srf_els + srf_gua + srf_hon + srf_nic if srf == .
	drop if location == "Costa Rica" | location == "Honduras" | location == "Nicaragua"

	/*UNPROFOR: Yugoslavia
	  Note: UNPROFOR was not deployed to the current Serbia, Montenegro, and Kosovo,
	        but here I use "Yugoslavia" as the location name.*/
	*count
	set obs 35
	replace location = "Yugoslavia" if location == ""
	sum srf if location == "Bosnia and Herzegovina"
	scalar srf_bih = r(mean)
	sum srf if location == "Croatia"
	scalar srf_cro = r(mean)
	sum srf if location == "North Macedonia"
	scalar srf_mac = r(mean)
	replace srf = srf_bih + srf_cro + srf_mac if srf == .


/*(d) Domestic areas*/
	/*Other 6 missions have names including domestic areas or multiple countries where they are deployed.
	  To adjust the size of peacekeeping expenditures for these missions, I use the size of domestic areas, not the size of whole countries.
	   - UNISFA  : Abyei
	   - MINURCAT: Central African Republic and Chad             -> Vakaga prefecture (Central African Republic), and Wadi Fira, Ouaddaï, Sila, Salamat regions (Chad)
	   - UNAMID  : Darfur
	   - UNTAES  : Eastern Slavonia, Baranja and Western Sirmium -> Eastern Slavonia and Baranja counties (Croatia)
	   - UNOMUR  : Uganda-Rwanda                                 -> Kabale and Kisoro districts (Uganda)
	   - MINURSO : Western Sahara
	   
	  UNPSG was replaced with UNTAES, and the website indicates the location for UNPSG is
	  Eastern Slavonia, Baranja and Western Sirmium (Danube region of Croatia), as with UNTAES.
	  (<https://peacekeeping.un.org/mission/past/cropol.htm> last access on Oct. 27, 2022)
	  Thus, I applied the adjustment also for UNPSG evn though the mission name does not include region names.
	   - UNPSG   : Eastern Slavonia, Baranja, and Western Sirmium
	  
	  UNMOP also includes a region name (the Prevlaka peninsula) in its mission name,
	  but the expenditures for UNMOP were all reported with UNMIBH. Thus, the surface area
	  of the Prevlaka will not be used for analysis (and the area size is missing actually).
	   - UNMOP   : Prevlaka*/

	*count
	set obs 43
	replace location = "Abyei"          in 36
	replace location = "CAR and Chad"   in 37
	replace location = "Darfur"         in 38
	replace location = "Eastern Slavonia, Baranja and Western Sirmium" in 39
	replace location = "Uganda-Rwanda"  in 40
	replace location = "Rwanda; Uganda-Rwanda" in 41
	replace location = "Western Sahara" in 42
	replace location = "Prevlaka"       in 43

	replace srf =  10546                                 if location == "Abyei" 
	/*IOM Abyei Report <https://reliefweb.int/sites/reliefweb.int/files/resources/Abyei%20VAS%20Report_31%20March%202017.pdf> last access on Oct. 27, 2022*/
	replace srf =  46500 + 52000 + 30000 + 36000 + 69000 if location == "CAR and Chad"
	/*Knoema - CAR <https://knoema.com/atlas/Central-African-Republic/Prefectures-profiles>;
	  City Population - Chad <http://www.citypopulation.de/en/chad/> last accesses on Oct. 27, 2022*/
	replace srf = 493180                                 if location == "Darfur"
	/*Web archive ? <https://web.archive.org/web/20111005071830/http://www.globaldreamers.org/holocaust/darfur/geography.html> last access on Oct. 27, 2022*/
	replace srf =   4155 +  2454                         if location == "Eastern Slavonia, Baranja and Western Sirmium"
	/*Statoids - Croatia <http://www.statoids.com/uhr.html> last access on Oct. 27, 2022*/
	replace srf =   1680 +   699                         if location == "Uganda-Rwanda"
	replace srf =  26340 +  1680 +   699                 if location == "Rwanda; Uganda-Rwanda"
	/*openAfrica - Uganda <https://africaopendata.org/dataset/size-of-uganda-districts-in-square-kilometers> last access on Oct. 27, 2022
	  Note: Financial resources for UNOMUR were sometimes reported with UNAMIR, so "Rwanda; Uganda-Rwanda" was also made for merge.*/
	replace srf = 266000                                 if location == "Western Sahara"
	/*Web archive for the CIA Factbook <https://web.archive.org/web/20200221102105/cia.gov/library/publications/the-world-factbook/geos/print_wi.html> last access on Oct. 27, 2022*/


	/*Other 6 missions may have been deployed only to some parts of domestic areas,
	  but I do not use the surface area of domestic areas to adjust their size
	  of peacekeeping expenditures because their mission names do not include the area names,
	  and then it is difficult to detect the exact area size of their activities. Just for reference:
	   - MINURCA : Central African Republic (Bangui) = Bangui city
	   - UNPREDEP: Macedonia (Northern region)       = Southeast, Skopje, and Polog regions + Debar, Ohrid, Struga, and Resen municipalities
	   - UNIFIL  : Lebanon (Southern region)         = Tyr, Marjaayoun, and Bint Jbeil districts
	   - UNOSOM  : Somalia (Southern region          = Somalia - (Somaliland + Puntland)
	   - UNOMIG  : Georgia (Western region/Tbilisi)  = Abkhazia, Samegrelo-Zemo Svaneti, and Tbilisi regions
	  
	  Strictly, UNPROFOR was deployed to Yugoslavia---BiH, Croatia, and Macedonia---
	  excluding the Southern regions of Macedonia, but I do not consider this for the scaling.
	   - UNPROFOR: Yugoslavia (strict)              = BiH + Croatia + Macedonia (Northern region)
	  
	  Lastly, just for notes, UNMOT was deployed to all of four divisions of Tajikistan:
	  Sughd, Region of Republican Subordination, Khatlon, and Gorno-Badakhshan
	  (Wikipedia - Regions of Tajikistan <https://en.wikipedia.org/wiki/Regions_of_Tajikistan> last access on Oct. 27, 2022).*/

	*count
	set obs 49
	replace location = "Central African Republic (Bangui)" in 44
	replace location = "Macedonia (Northern region)"       in 45
	replace location = "Lebanon (Southern region)"         in 46
	replace location = "Somalia (Southern region)"         in 47
	replace location = "Georgia (Western region/Tbilisi)"  in 48
	replace location = "Yugoslavia (strict)"               in 49

	replace srf =     67                                           if location == "Central African Republic (Bangui)"
	/*Knoema - Bangui <https://knoema.com/atlas/Central-African-Republic/Bangui> last access on Oct. 27, 2022*/
	replace srf =   2739 +   1813 +   2416 + 146 + 390 + 483 + 551 if location == "Macedonia (Northern region)"
	/*City Population - Macedonia <http://www.citypopulation.de/en/northmacedonia/cities/> last access on Oct. 27, 2022*/
	replace srf =    418 +    313 +    260                         if location == "Lebanon (Southern region)"
	/*Localiban - Lebanon <http://www.localiban.org/lebanon> last access on Oct. 27, 2022*/
	replace srf = 637660 - 176120 - 212510                         if location == "Somalia (Southern region)"
	/*Candlelight, Somaliland <https://land.igad.int/index.php/documents-1/countries/somalia/conflict-4/878-impact-of-civil-war-on-natural-resources-a-case-study-for-somaliland/file> last access on Oct. 27, 2022*/
	replace srf =   8660 +   7441 +    504                         if location == "Georgia (Western region/Tbilisi)"
	/*Wikipedia - Admin divisions of Georgia <https://en.wikipedia.org/wiki/Administrative_divisions_of_Georgia_(country)>*/
	replace srf =  51210 +  56540 +   8538                         if location == "Yugoslavia (strict)"


/*(e) Interstate conflicts*/
	*count
	set obs 59
	replace location = "Afghanistan and Pakistan" in 50 /*UNGOMAP*/
	replace location = "Ethiopia and Eritrea"     in 51 /*UNMEE*/
	replace location = "Golan"                    in 52 /*UNDOF*/
	replace location = "India and Pakistan"       in 53 /*UNMOGIP*/
	replace location = "Iran and Iraq"            in 54 /*UNIIMOG*/
	replace location = "Iraq and Kuwait"          in 55 /*UNIKOM*/
	replace location = "Aouzou Strip"             in 56 /*UNASOG*/
	replace location = "West New Guinea"          in 57 /*UNSF*/
	replace location = "Suez"                     in 58 /*UNEF I*/
	replace location = "Suez; Sinai"              in 59 /*UNEF II*/
	/*Note 1: UNTSO is the only mission deployed to a region, Middle East.
			  The mission (and the area size of Middle East) is not included
			  in this adjustment factors dataset.
	  Note 2: Most of missions for interstate conflicts are deployed only to
			  the common border of the countries in a given conflict.
			  The size of border areas is difficult to know, and thus,
			  they are remained as missing (I do not use for analysis tho).*/

	replace srf = 114000 if location == "Aouzou Strip"
	/*Britannica - Aouzou Strip <https://www.britannica.com/place/Aozou-Strip> last access on Oct. 27, 2022*/
	replace srf = 420540 if location == "West New Guinea"
	/*New World Encyclopedia - West New Guinea <https://www.newworldencyclopedia.org/entry/Western_New_Guinea> last access on Oct. 27, 2022*/


/*(f) Others*/
	rename srf srf_location
	gen ln_srf_location = ln(srf_location)
	replace location = "Democratic Republic of the Congo"          if location == "Congo, Dem. Rep."
	replace location = "Congo"                                     if location == "Congo, Rep."
	replace location = "Côte d'Ivoire"                             if location == "Cote d'Ivoire"
	replace location = "The former Yugoslav Republic of Macedonia" if location == "North Macedonia"
	replace location = "Yemen"                                     if location == "Yemen, Rep."
	sort location
	save "Data/02. Peacekeeping/UNPO4_surface_areas_for_adjustment.dta", replace



*------------------------------------------------------------------------------*
/*(5) Adjustment factor 2: population                                         */
*------------------------------------------------------------------------------*
/*(a) Only UN PKO host countries*/
	import excel using "RawData/03. Controls/World Bank/API_SP.POP.TOTL_DS2_en_excel_v2_713231.xls", sheet("Data") clear
	local i = 1960
	foreach var of varlist E-BL {
		rename `var' pop`i'
		local i = `i' + 1
	}
	rename A location
	drop if _n <= 4
	drop B-D
	reshape long pop, i(location) j(year)
	destring, replace

	keep if ///
		location == "Angola"                   | location == "Bosnia and Herzegovina" | ///
		location == "Burundi"                  | location == "Cambodia"               | ///
		location == "Central African Republic" | location == "Chad"                   | ///
		location == "Congo, Dem. Rep."         | location == "Congo, Rep."            | ///
		location == "Cote d'Ivoire"            | location == "Costa Rica"             | ///
		location == "Croatia"                  | location == "Cyprus"                 | ///
		location == "Dominican Republic"       | location == "El Salvador"            | ///
		location == "Georgia"                  | location == "Guatemala"              | ///
		location == "Haiti"                    | location == "Honduras"               | ///
		location == "Kosovo"                   | location == "Lebanon"                | ///
		location == "Liberia"                  | location == "Mali"                   | ///
		location == "Mozambique"               | location == "Namibia"                | ///
		location == "Nicaragua"                | location == "North Macedonia"        | ///
		location == "Rwanda"                   | location == "Sierra Leone"           | ///
		location == "Somalia"                  | location == "South Africa"           | ///
		location == "South Sudan"              | location == "Sudan"                  | ///
		location == "Syrian Arab Republic"     | location == "Tajikistan"             | ///
		location == "Timor-Leste"              | location == "Yemen, Rep."
	*tab location if pop == . /*1 * 36 countries*/
	*tab year if pop == .     /*Population data is missing for all the countries in 2019, but only in 2019.*/
	drop if year == 2019	  /*No missing data for host countries (not including domestic areas).*/


/*(b) Multi-country regions*/
	/*UNMIS: Sudan + South Sudan*/
	foreach num of numlist 1960/2018 {
		sum pop if location == "South Sudan" & year == `num'
		replace pop = pop + r(mean) if location == "Sudan" & year == `num'
	}

	/*ONUCA: Central America*/
	*count
	set obs 2183
	replace location = "Central America" if location == ""
	local i = 2125
	foreach num of numlist 1960/2018 {
		replace year = `num' in `i'
		local i = `i' + 1
		
		sum pop if location == "Costa Rica" & year == `num'
		scalar pop_cos = r(mean)
		sum pop if location == "El Salvador" & year == `num'
		scalar pop_els = r(mean)
		sum pop if location == "Guatemala" & year == `num'
		scalar pop_gua = r(mean)
		sum pop if location == "Honduras" & year == `num'
		scalar pop_hon = r(mean)
		sum pop if location == "Nicaragua" & year == `num'
		scalar pop_nic = r(mean)
		replace pop = pop_cos + pop_els + pop_gua + pop_hon + pop_nic if pop == . & year == `num'
	}
	drop if location == "Costa Rica" | location == "Honduras" | location == "Nicaragua"

	/*Yugoslavia = BiH + Croatia + Macedonia*/
	*count
	set obs 2065
	replace location = "Yugoslavia" if location == ""
	local i = 2007
	foreach num of numlist 1960/2018 {
		replace year = `num' in `i'
		local i = `i' + 1
		
		sum pop if location == "Bosnia and Herzegovina" & year == `num'
		scalar pop_bih = r(mean)
		sum pop if location == "Croatia" & year == `num'
		scalar pop_cro = r(mean)
		sum pop if location == "North Macedonia" & year == `num'
		scalar pop_mac = r(mean)
		replace pop = pop_bih + pop_cro + pop_mac if pop == . & year == `num'
	}


/*(c) Domestic areas / Intersate conflicts:
	  Population data over time can be found only for Western Sahara.
	  Because there are a certain amount of missing values of popuation for domestic areas,
	  I use the surface area instead for adjustment of peacekeeping expenditures.*/

	/*Missing values for domestic areas / interstate conflicts other than Western Sahara*/
	*count
	set obs 3422
	local i = 2066
	foreach x in ///
		"Central African Republic (Bangui)" "Macedonia (Northern region)" "Lebanon (Southern region)" ///
		"Somalia (Southern region)" "Georgia (Western region/Tbilisi)" "Yugoslavia (strict)"          ///
		"Abyei" "CAR and Chad" "Darfur" "Eastern Slavonia, Baranja and Western Sirmium" "Uganda-Rwanda" ///
		"Rwanda; Uganda-Rwanda" "Prevlaka" "Afghanistan and Pakistan" "Ethiopia and Eritrea" "Golan"  ///
		"India and Pakistan" "Iran and Iraq" "Iraq and Kuwait" "Aouzou Strip" "West New Guinea" "Suez" "Suez; Sinai" {
		local j = `i' + 58
		replace year = 1960 if _n == `i'
		replace year = year[_n-1] + 1 if _n > `i' & _n <= `j'
		replace location = "`x'" if _n >= `i' & _n <= `j'
		local i = `i' + 59
	}

	/*Western Sahara:
	  Worldometer - Western Sahara <https://www.worldometers.info/world-population/western-sahara-population/> last access on Oct. 27, 2022.
	  However, there are still missing values: mostly, population data for Western Sahara exist every five years.*/
	*count
	set obs 3481
	replace location = "Western Sahara" if location == ""
	bysort location: gen x =  _n
	local i = 1
	foreach num of numlist 1960/2018 {
		replace year = `num' if year == . & x == `i'
		local i = `i' + 1
	}
	drop x

	replace pop =  32761 if location == "Western Sahara" & year == 1960
	replace pop =  50970 if location == "Western Sahara" & year == 1965
	replace pop =  76874 if location == "Western Sahara" & year == 1970
	replace pop =  74954 if location == "Western Sahara" & year == 1975
	replace pop = 150877 if location == "Western Sahara" & year == 1980
	replace pop = 182421 if location == "Western Sahara" & year == 1985
	replace pop = 217258 if location == "Western Sahara" & year == 1990
	replace pop = 255634 if location == "Western Sahara" & year == 1995
	replace pop = 314118 if location == "Western Sahara" & year == 2000
	replace pop = 437515 if location == "Western Sahara" & year == 2005
	replace pop = 480274 if location == "Western Sahara" & year == 2010
	replace pop = 526216 if location == "Western Sahara" & year == 2015
	replace pop = 538749 if location == "Western Sahara" & year == 2016
	replace pop = 552615 if location == "Western Sahara" & year == 2017
	replace pop = 567402 if location == "Western Sahara" & year == 2018


/*(d) Others*/
	rename pop pop_location
	gen ln_pop_location = ln(pop_location)
	replace location = "Democratic Republic of the Congo"          if location == "Congo, Dem. Rep."
	replace location = "Congo"                                     if location == "Congo, Rep."
	replace location = "Côte d'Ivoire"                             if location == "Cote d'Ivoire"
	replace location = "The former Yugoslav Republic of Macedonia" if location == "North Macedonia"
	replace location = "Yemen"                                     if location == "Yemen, Rep."
	sort location year
	save "Data/02. Peacekeeping/UNPO5_population_for_adjustment.dta", replace



*------------------------------------------------------------------------------*
/*(6) UN PKO financial resources                                              */
*------------------------------------------------------------------------------*
/*(6-1) Mission-budgetary period data (raw data)                              */
*------------------------------------------------------------------------------*
	import excel using "RawData/02. Peacekeeping/PKOF_v1.xlsx", firstrow clear
	destring, replace
	drop *Rexp *Nrexp Rdp* COETransport RSCE*

/*(a) Replace missing values with 0*/
	gen onlyapp = 0
	replace onlyapp = 1 if ///
		DocumentSymbol == "A/54/807" | ///
		(DocumentSymbol == "A/58/370" & month(PeriodFrom) == 7)
	gen onlyexp = 0
	replace onlyexp = 1 if ///
		(DocumentSymbol == "A/45/802" & year(PeriodFrom) == 1989) | ///
		(DocumentSymbol == "A/48/701" & year(PeriodFrom) == 1993) | ///
		(DocumentSymbol == "A/58/370" & month(PeriodFrom) == 5) | ///
		 DocumentSymbol == "A/59/745" | ///
		 DocumentSymbol == "A/59/748" | ///
		 DocumentSymbol == "A/60/651" | ///
		 DocumentSymbol == "A/67/707" | ///
		 DocumentSymbol == "A/68/597"
	foreach x in Milob Milcon Milother UNpol FPU ///
		Intlcon UNV Govt Election Gross Net Total Mil Civ Ops {
		recode `x'App (.=0) if onlyexp == 0
		recode `x'Exp (.=0) if onlyapp == 0
		recode `x'Var (.=0) if onlyapp == 0 & onlyexp == 0
	}
	foreach x in Civintl Civnat Consul Temporary Common {
		recode `x'App (.=0) if onlyexp == 0 & CivstaffApp == .
		recode `x'Exp (.=0) if onlyapp == 0 & CivstaffExp == .
		recode `x'Var (.=0) if onlyapp == 0 & onlyexp == 0 & CivstaffVar == .
	}


/*(b) Make variables*/
	/*Session #*/
	gen Session = real(regexs(2)) if regexm(DocumentSymbol, "(^A/([0-9][0-9]))")
	replace Session = real(regexs(2)) if regexm(DocumentSymbol, "(^A/C.5/([0-9][0-9]))")

	/*Military component*/
	gen Military_Bdg = MilobApp + MilconApp + MilotherApp
	replace Military_Bdg = MilobApp + MilconApp + DeathApp if Session <= 56 & MilotherApp == 0 & DeathApp != . /*UNMIBH and ONUMOZ are not applicable, but their death and disability awards = 0.*/
	gen Military_Exp = MilobExp + MilconExp + MilotherExp
	replace Military_Exp = MilobExp + MilconExp + DeathExp if Session <= 56 & MilotherExp == 0 & DeathExp != .
	gen Military_Var = Military_Bdg - Military_Exp

	/*Police component*/
	gen Police_Bdg = UNpolApp + FPUApp
	gen Police_Exp = UNpolExp + FPUExp
	gen Police_Var = Police_Bdg - Police_Exp

	/*Civilian component*/
	gen Civilian_Bdg = ///
		CivintlApp + CivnatApp + UNVApp + TemporaryApp + GovtApp + ///
		ElectionApp + IntlconApp + ConsulApp + StaffassessApp ///
		if CivstaffApp == . & Session <= 56
	replace Civilian_Bdg = ///
		CivintlApp + CivnatApp + UNVApp + TemporaryApp + GovtApp + ///
		ElectionApp + IntlconApp + ConsulApp ///
		if CivstaffApp == . & Session >= 57
	replace Civilian_Bdg = ///
		CivstaffApp + UNVApp + GovtApp + ElectionApp + ///
		IntlconApp + StaffassessApp if CivstaffApp != .
	gen Civilian_Exp = ///
		CivintlExp + CivnatExp + UNVExp + TemporaryExp + GovtExp + ///
		ElectionExp + IntlconExp + ConsulExp + StaffassessExp ///
		if CivstaffExp == . & Session <= 56
	replace Civilian_Exp = ///
		CivintlExp + CivnatExp + UNVExp + TemporaryExp + GovtExp + ///
		ElectionExp + IntlconExp + ConsulExp ///
		if CivstaffExp == . & Session >= 57
	replace Civilian_Exp = ///
		CivstaffExp + UNVExp + GovtExp + ElectionExp + ///
		IntlconExp + StaffassessExp if CivstaffExp != .
	gen Civilian_Var = Civilian_Bdg - Civilian_Exp

	/*Gross requirements*/
	gen Gross_Bdg = Military_Bdg + Police_Bdg + Civilian_Bdg + OpsApp + OtherprogApp
	replace Gross_Bdg = Military_Bdg + Police_Bdg + Civilian_Bdg + OpsApp if OtherprogApp == .
	replace Gross_Bdg = Military_Bdg + Police_Bdg + Civilian_Bdg + OpsApp + OtherprogApp + VolconbdApp if VolconbdApp < 0
	gen Gross_Exp = Military_Exp + Police_Exp + Civilian_Exp + OpsExp + OtherprogExp
	replace Gross_Exp = Military_Exp + Police_Exp + Civilian_Exp + OpsExp if OtherprogExp == .
	replace Gross_Exp = Military_Exp + Police_Exp + Civilian_Exp + OpsExp + OtherprogExp + VolconbdExp if VolconbdExp < 0
	gen Gross_Var = Gross_Bdg - Gross_Exp

	/*Net requirements*/
	gen Net_Bdg = Gross_Bdg - StaffassessApp
	gen Net_Exp = Gross_Exp - StaffassessExp
	gen Net_Var = Net_Bdg - Net_Exp

	/*Total requirements*/
	gen Total_Bdg = Net_Bdg + VolconbdApp + VolconnbdApp
	replace Total_Bdg = Net_Bdg + VolconApp if Total_Bdg == . & q2 == 1
	replace Total_Bdg = Net_Bdg - VolconbdApp + VolconnbdApp if VolconbdApp < 0
	gen Total_Exp = Net_Exp + VolconbdExp + VolconnbdExp
	replace Total_Exp = Net_Exp + VolconExp if Total_Exp == . & q2 == 1
	replace Total_Exp = Net_Exp - VolconbdExp + VolconnbdExp if VolconbdExp < 0
	gen Total_Var = Total_Bdg - Total_Exp

	/*Other (sub)categories*/
	foreach x in ///
		Milob Milcon Milother UNpol FPU Civintl Civnat Civstaff  ///
		Intlcon Consul UNV Temporary Common Govt Election        ///
		Ops Death Otherprog UNLB Support Staffassess Otherincome ///
		Volconnbd Volconbd Volcon {
		rename `x'App `x'_Bdg
		rename `x'Exp `x'_Exp
		gen `x'_Var = `x'_Bdg - `x'_Exp
	}

	gen PeriodDays = PeriodTo - PeriodFrom + 1


/*(c) Others*/
	keep MissionAbbrev-PeriodMonths PeriodDays Session *_Bdg *_Exp *_Var MajTotal SelfTotal Other Nbd q8
	rename q8 Withdrawal
	rename *_* **
	order MissionAbbrev-PeriodMonths PeriodDays Session MilitaryBdg-CivilianVar Ops* GrossBdg-TotalVar ///
		Milob* Milcon* Milother* UNpol* FPU* Civintl* Civnat* Civstaff* ///
		Intlcon* Consul* UNV* Temporary* Common* Govt* Election*        ///
		Ops* Death* Otherprog* UNLB* Support* Staffassess* Otherincome* ///
		Volconnbd* Volconbd* Volcon*, first
	foreach var of varlist * {
		label variable `var' ""
	}
	save "Data/02. Peacekeeping/UNPO6_financial_resources_base.dta", replace



*------------------------------------------------------------------------------*
/*(6-2) Mission-month data (on a daily average; adjusted)                     */
*------------------------------------------------------------------------------*
/*(a) Unit: Budgetary period -> month*/
	use "Data/02. Peacekeeping/UNPO6_financial_resources_base.dta", clear
	gen expand = ym(year(PeriodTo), month(PeriodTo)) - ym(year(PeriodFrom), month(PeriodFrom)) + 1
	expand expand

	bysort MissionAbbrev PeriodFrom: gen x = _n
	gen ym = ym(year(PeriodFrom), month(PeriodFrom)) if x == 1
	by MissionAbbrev PeriodFrom: replace ym = ym[_n-1] + 1 if x != 1

	gen month = month(PeriodFrom) if x == 1
	replace month = month[_n-1] + 1 if x != 1
	count if month > 12
	while r(N) > 0 {
		replace month = month - 12 if month > 12
		count if month > 12
	}

	gen year = year(PeriodFrom) if x == 1
	replace year = year[_n-1] if x != 1 & month[_n-1] != 12
	count if year == .
	while r(N) > 0 {
		by MissionAbbrev PeriodFrom: replace year = year[_n-1] + 1 if month[_n-1] == 12
		by MissionAbbrev PeriodFrom: replace year = year[_n-1] if year == . & month[_n-1] != 12
		count if year == .
	}
	drop expand x


/*(b) Expenditures: Budgetary year sum -> daily average*/
	/*# of days with UN PKO expenditures*/
	gen length_all = PeriodTo - PeriodFrom + 1
	gen length_active = mdy(month(PeriodFrom)+1, 1, year(PeriodFrom)) ///
		- mdy(month(PeriodFrom), day(PeriodFrom), year(PeriodFrom)) ///
		if ym(year, month) == ym(year(PeriodFrom), month(PeriodFrom)) & month != 12
	replace length_active = mdy(1, 1, year(PeriodFrom)+1) ///
		- mdy(month(PeriodFrom), day(PeriodFrom), year(PeriodFrom)) ///
		if ym(year, month) == ym(year(PeriodFrom), month(PeriodFrom)) & month == 12
	replace length_active = day(PeriodTo) ///
		if ym(year, month) == ym(year(PeriodTo), month(PeriodTo))
	replace length_active = mdy(month+1, 1, year) - mdy(month, 1, year) ///
		if length_active == . & month != 12
	replace length_active = mdy(1, 1, year+1) - mdy(month, 1, year) ///
		if length_active == . & month == 12
	*by MissionAbbrev PeriodFrom: egen sum_length_active = sum(length_active)
	*count if length_all != sum_length_active
	*drop sum_length_active

	/*Expenditures allocated to each month:
	  Assuming that UN PKO expenditures are spent on the same amount every day,
	  the allocated amount of expenditures to each month can be calculated
	  based on the % of days to the whole length of the budgetary period.*/
	gen length_pct = length_active / length_all
	foreach var of varlist MilitaryBdg-Nbd {
		replace `var' = `var' * length_pct
	}

	/*Daily average:
	  Because I aggregate the expenditures by the # of days for each month,
	  the amount increases as the length of months (e.g., fewer in February).
	  Thus, the daily aggregated expenditures will be the daily average per month.*/
	gen length_month = mdy(month+1, 1, year) - mdy(month, 1, year) if month != 12
	replace length_month = mdy(1, 1, year+1) - mdy(month, 1, year) if month == 12
	foreach var of varlist MilitaryBdg-Nbd {
		replace `var' = `var' / length_month
	}
	drop length*

	/*Aggregate the same mission-months:
	  Some obs indicate the same mission-months because budgetary periods started
	  and ended in the middle of a month. Suppose that mission A has $500 until the 15th
	  and $1000 from the 16th to 31st in January. The daily average is calculated
	  ($500/31)+($1000/31) = $500+$1000/31 = $1,500/31, which means I can just aggregate the obs!*/
	*bysort MissionAbbrev ym: gen dup = _N
	*tab dup, m /*44 obs overlap the same mission-months.*/
	*drop dup
	sort MissionAbbrev ym
	drop MissionType RegionName Document* Period* Session
	foreach var of varlist MilitaryBdg-Nbd {
		by MissionAbbrev ym: egen missing = sum(missing(`var'))
		by MissionAbbrev ym: egen sum_`var' = sum(`var') if missing == 0
		replace `var' = sum_`var'
		drop sum_`var' missing
	}
	duplicates drop MissionAbbrev ym, force


/*(c) Adjustment factors (surface area / population)*/
	/*For merge*/
	gen location = CountryName
	replace location = TerritoryName if TerritoryName != ""
	replace location = "CAR and Chad"          if MissionAbbrev == "MINURCAT"
	replace location = "Central America"       if MissionAbbrev == "ONUCA"
	replace location = "Eastern Slavonia, Baranja and Western Sirmium" if MissionAbbrev == "UNTAES" | MissionAbbrev == "UNPSG"
	replace location = "Ethiopia and Eritrea"  if MissionAbbrev == "UNMEE"
	replace location = "Iran and Iraq"         if MissionAbbrev == "UNIIMOG"
	replace location = "Iraq and Kuwait"       if MissionAbbrev == "UNIKOM"
	replace location = "Uganda-Rwanda"         if MissionAbbrev == "UNOMUR"
	replace location = "Rwanda; Uganda-Rwanda" if MissionAbbrev == "UNAMIR" & Mission2Abbrev == "UNOMUR"
	replace location = "Yugoslavia"            if MissionAbbrev == "UNPROFOR"

	/*Surface areas*/
	merge m:1 location using "Data/02. Peacekeeping/UNPO4_surface_areas_for_adjustment.dta"
	*tab location if _merge == 2 /*Missions in Croatia were deployed to the domestic areas, or the expenditures were reported together with UNPROFOR.*/
	replace    srf_location = . if MissionAbbrev == "UNAMIS" | MissionAbbrev == "UNOMSA" /*UNAMIS and UNOMSA are not PKOs nor used for analysis.*/
	replace ln_srf_location = . if MissionAbbrev == "UNAMIS" | MissionAbbrev == "UNOMSA"
	drop if _merge == 2
	drop _merge

	/*Population*/
	merge m:1 location year using "Data/02. Peacekeeping/UNPO5_population_for_adjustment.dta"
	*tab year if _merge == 1 /*2019 is only included in the financial resources data.*/
	replace    pop_location = . if MissionAbbrev == "UNAMIS" | MissionAbbrev == "UNOMSA"
	replace ln_pop_location = . if MissionAbbrev == "UNAMIS" | MissionAbbrev == "UNOMSA"
	drop if _merge == 2
	drop _merge

	sort location ym
	foreach var of varlist MilitaryBdg-Nbd {
		gen    srf_`var' = `var'/srf_location
		gen ln_srf_`var' = `var'/ln_srf_location
		gen    pop_`var' = `var'/pop_location
		gen ln_pop_`var' = `var'/ln_pop_location
	}
	save "Data/02. Peacekeeping/UNPO6_financial_resources_mission-month.dta", replace



*------------------------------------------------------------------------------*
/*(6-3) Conflict-month data (on a daily average; adjusted)                    */
*------------------------------------------------------------------------------*
/*(a) Mission-month data*/
	use "Data/02. Peacekeeping/UNPO1_mission_list.dta", clear /*Don't use UNPO2 because the unit of data for UNPO2 is not a mission-month, but a month (with at least one PKO).*/
	keep if MissionType == "PKO"
	drop MissionType

	gen expand = ym(EndYear, EndMonth) - ym(StartYear, StartMonth) + 1 if Ongoing == 0
	replace expand = ym(2019,12) - ym(StartYear, StartMonth) + 1 if Ongoing == 1
	expand expand

	bysort MissionAbbrev: gen x = _n
	gen ym = ym(StartYear, StartMonth) if x == 1
	by MissionAbbrev: replace ym = ym[_n-1] + 1 if x != 1

	gen month = StartMonth if x == 1
	replace month = month[_n-1] + 1 if x != 1
	count if month > 12
	while r(N) > 0 {
		replace month = month - 12 if month > 12
		count if month > 12
	}

	gen year = StartYear if x == 1
	replace year = year[_n-1] if x != 1 & month[_n-1] != 12
	count if year == .
	while r(N) > 0 {
		by MissionAbbrev: replace year = year[_n-1] + 1 if month[_n-1] == 12
		by MissionAbbrev: replace year = year[_n-1] if year == . & month[_n-1] != 12
		count if year == .
	}
	drop expand x StartYear-EndDay ChapterVII Ongoing


/*(b) Merge mission-month data of UN PKO expenditures*/
	merge 1:1 MissionAbbrev year month ym using "Data/02. Peacekeeping/UNPO6_financial_resources_mission-month.dta"
	drop if MissionAbbrev == "UNAMIS" | MissionAbbrev == "UNOMSA"
	drop if ym >= ym(2019,7)

	*tab ym if _merge == 1
	*tab MissionAbbrev if _merge == 1
	tab MissionAbbrev if _merge == 1 & ///
		(MissionAbbrev != "DOMREP"  & MissionAbbrev != "ONUC"    & ///
		 MissionAbbrev != "UNASOG"  & MissionAbbrev != "UNEF I"  & ///
		 MissionAbbrev != "UNEF II" & MissionAbbrev != "UNGOMAP" & ///
		 MissionAbbrev != "UNIPOM"  & MissionAbbrev != "UNOGIL"  & ///
		 MissionAbbrev != "UNSF"    & MissionAbbrev != "UNYOM"   & ///
		 MissionAbbrev != "UNMOGIP" & MissionAbbrev != "UNTSO")

	/*Without financial resources data, but in the mission list:
	  There are four cases.
	   - Establishment month of a given mission
	   - Replacement month to another mission
	   - Financial resources for a given mission are reported together with others
	   - Time periods before the financial resources dataset covers

	  Listed are the cases.
	   - MINURSO;   Apr. 1991: established on Apr. 29, 1991
	   - UNAMIC;    Oct. 1991: established on Oct. 16, 1991
	   - UNAVEM II; May  1991: replaced with UNAVEM I on May 31, 1991
	   - UNCRO               : financial resources are included in UNPROFOR (Mar. 1995-Jan. 1996)
	   - UNDOF               : before the time period my dataset covers (-Nov. 1989)
	   - UNFICYP             : before the time period my dataset covers (-May 1993)
	   - UNIFIL              : before the time period my dataset covers (-Jan. 1989)
	   - UNISFA;    Jun. 2011: established on Jun. 27, 2011
	   - UNMIBH;    Dec. 1995: established on Dec. 21, 1995
	   - UNMISET             : established on May 17, 2002
	   - UNMOP               : financial resources are included in UNMIBH (Feb. 1996-Dec. 2002)
	   - UNOMSIL             : financial resources are included in UNAMSIL (Jul.-Dec. 1999)
	   - UNOMUR              : financial resources are included in UNAMIR (Jan.-Sep. 1994)
	   - UNOSOM II           : financial resources are included in UNOSOM I (Mar.-Apr. 1993) / established on Mar. 26, 1993
	   - UNPREDEP            : established on Mar. 31, 1995 / financial resources are included in UNPROFOR (Apr. 1995-Jun. 1997)
	   - UNPSG               : financial recources are included in UNTAES (Jan.-Jun. 1998)
	   - UNSMIH              : replaced with UNTMIH in Jul. 1997
	   - UNTAC               : financial resources are included in UNAMIC (Mar. 1992-Aug. 1993)
	   - UNTAET              : financial resources are included in UNAMET (Oct.-Nov. 1999) / since UNAMET is a SPM, financial resources for these two months are not reflected in my dataset.
	   - UNTMIH              : financial resources are included in MIPONUH (Aug.-Nov. 1997) / succeeded from UNSMIH in Jul. 1997*/

	tab MissionAbbrev if _merge == 2
	/*With financial resources data, but not in the mission list:
	  There are three cases.
	   - Replcaement with other missions
	   - Before a given mission became operated
	   - After a given mission was terminated

	  Listed are the cases.
	   - MINUCI   : replaced with UNOCI (May-Jun. 2004)
	   - MINUGUA  : before this mission became operated (Feb. 1997) / established in Jan. 1997
	   - MINURCA  : before this mission became opeeated (Mar. 1998) / established in Mar. 1998
					replace with BONUCA (Mar.-Jun. 2000)
	   - MINURCAT : before this mission was established (Jul.-Aug. 2007)
					after the mission was terminated (Jan.-Jun. 2011)
	   - MINUSMA  : between establishment and starting operation (Apr.-Jun. 2013)
	   - MINUSTAH : between establishment and starting operation (May 2004)
					replaced with MINUJUSTH (Nov. 2017-Jun. 2018)
	   - MIPONUH  : replaced with UNTMIH (Jul.-Nov. 1997) / financial resources of UNTMIH are all included in MIPONUH
					replaced with MICAH (Apr.-Jun. 2000)
	   - MONUA    : replaced with UNOA (Mar. 1999-Jun. 2000) / the last financial report for MONUA is all for liquidation
	   - MONUC    : before this mission was established (Aug.-Oct. 1999)
	   - ONUB     : between establishment and starting operation (Apr.-May 2004)
					replaced with BINUB (Jan.-Jun. 2007)
	   - ONUCA    : after this mission was terminated (Feb.-Apr. 1992)
	   - ONUMOZ   : before this mission was established (Oct.-Nov. 1992)
					after this mission was terminated (Jan.-Mar. 1995)
	   - UNAMIC   : replaced with UNTAC (Apr. 1992-Aug. 1993)
	   - UNAMIR   : after this mission was terminated (Apr. 1996-Jun. 1997)
	   - UNAMSIL  : replaced with UNOMSIL (Jul.-Nov. 1999)
					replaced with UNIOSIL (Jan.-Jun. 2006)
	   - UNAVEM I : replaced with UNAVEM II (Jun.-Aug. 1991)
	   - UNIKOM   : after this mission was terminated (Nov. 2003)
	   - UNMEE    : after this mission was terminated (Aug. 2008-Jun. 2009)
	   - UNMIBH   : after this mission was terminated (Jan.-Jun. 2003)
	   - UNMIL    : replaced with UNOL (Aug.-Sep. 2003)
					after this mission was terminated (Apr.-Jun. 2018)
	   - UNMIS    : replaced with UNAMIS (Jul. 2004-Feb. 2005)
					replaced with UNAMID (Aug. 2011-Jun. 2012)
	   - UNMISET  : replaced with UNOTIL (Jun.-Oct. 2005)
	   - UNMIT    : after this mission was terminated (Jan.-Jun. 2013)
	   - UNMOT    : after this mission was terminated (Jun. 2000)
	   - UNOMIG   : after this mission was terminated (Jul. 2009-Jun. 2010) / the last financial report for UNOMIG is all for liquidation
	   - UNOMIL   : replaced with UNOL (Oct. 1997-Jun. 1998)
	   - UNOSOM I : replaced with UNOSOM II (Apr. 1993)
	   - UNOSOM II: replaced with UNPOS (Apr.-Jun. 1995)
	   - UNPREDEP : after this mission was terminated (Mar.-Jun. 1999)
	   - UNPROFOR : before this mission became operated (Jan. 1992)
					after this mission was terminated (Jan. 1996-Jun. 1997)
	   - UNPSG    : after this mission was terminated (Nov. 1998-Jun. 1999)
	   - UNSMIS   : after this mission was terminated (Sep. 2012-Jun. 2013)
	   - UNTAES   : replaced with UNPSG (Feb.-Jun. 1998)
	   - UNTAET   : replaced with UNMISET (Jun. 2002)*/

	/*Replace missing values when the obs are only in the financial resources data:
	  I do not drop missions that _merge == 2 and are listed above.
	  This means that the effect of peacekeeping expenditures includes the effect of
	  when a mission does not become operated or when a mission already ended,
	  but in those periods, the expenditures were still spent according to my dataset.*/
	sort MissionAbbrev ym
	by MissionAbbrev: replace UCDPNewID = UCDPNewID[_n-1] if UCDPNewID == "" & _merge == 2
	foreach var of varlist CountryName-Territory3Name location-ln_srf_location {
		gen missing = missing(`var')
		by MissionAbbrev: replace `var' = `var'[_n-1] if missing == 1 & _merge == 1
		drop missing
	}
	by MissionAbbrev: replace    pop_location =    pop_location[_n-1] if    pop_location == . & _merge == 1 & year == year[_n-1] /*Population is time-varying.*/
	by MissionAbbrev: replace ln_pop_location = ln_pop_location[_n-1] if ln_pop_location == . & _merge == 1 & year == year[_n-1]

	gsort MissionAbbrev -ym
	by MissionAbbrev: replace UCDPNewID = UCDPNewID[_n-1] if UCDPNewID == "" & _merge == 2
	foreach var of varlist CountryName-Territory3Name location-ln_srf_location {
		gen missing = missing(`var')
		by MissionAbbrev: replace `var' = `var'[_n-1] if missing == 1 & _merge == 1
		drop missing
	}
	by MissionAbbrev: replace    pop_location =    pop_location[_n-1] if    pop_location == . & _merge == 1 & year == year[_n-1]
	by MissionAbbrev: replace ln_pop_location = ln_pop_location[_n-1] if ln_pop_location == . & _merge == 1 & year == year[_n-1]
	drop _merge Withdrawal


/*(c) Put values for missing data (just to complete ...)*/
	replace CountryName = "Dominican Republic"    if MissionAbbrev == "DOMREP"
	replace CountryName = "Congo"                 if MissionAbbrev == "ONUC"
	replace CountryName = "Chad"                  if MissionAbbrev == "UNASOG"
	replace CountryName = "Croatia"               if MissionAbbrev == "UNCRO"
	replace CountryName = "Egypt, Israel"         if MissionAbbrev == "UNEF I" | MissionAbbrev == "UNEF II"
	replace CountryName = "Afghanistan, Pakistan" if MissionAbbrev == "UNGOMAP"
	replace CountryName = "India, Pakistan"       if MissionAbbrev == "UNIPOM" | MissionAbbrev == "UNMOGIP"
	replace CountryName = "Croatia"               if MissionAbbrev == "UNMOP"
	replace CountryName = "Lebanon"               if MissionAbbrev == "UNOGIL"
	replace CountryName = "Indonesia"             if MissionAbbrev == "UNSF"
	replace CountryName = "Haiti"                 if MissionAbbrev == "UNTMIH"
	replace CountryName = "Yemen"                 if MissionAbbrev == "UNYOM"

	replace TerritoryName = "Aouzou Strip"    if MissionAbbrev == "UNASOG"
	replace TerritoryName = "Suez"            if MissionAbbrev == "UNEF I"
	replace TerritoryName = "Suez; Sinai"     if MissionAbbrev == "UNEF II"
	replace TerritoryName = "Prevlaka"        if MissionAbbrev == "UNMOP"
	replace TerritoryName = "West New Guinea" if MissionAbbrev == "UNSF"

	replace location = CountryName if location == ""
	replace location = "Afghanistan and Pakistan" if MissionAbbrev == "UNGOMAP"
	replace location = "India and Pakistan"       if MissionAbbrev == "UNIPOM" | MissionAbbrev == "UNMOGIP"
	replace location = TerritoryName if MissionAbbrev == "UNASOG" | MissionAbbrev == "UNEF I" | MissionAbbrev == "UNEF II" | MissionAbbrev == "UNMOP" | MissionAbbrev == "UNSF"

	merge m:1 location using "Data/02. Peacekeeping/UNPO4_surface_areas_for_adjustment.dta", update
	*tab MissionAbbrev if _merge == 1 /*UNTSO*/
	*tab location if _merge == 2      /*Deployed to domestic areas, but the surface areas are not used for analysis*/
	*tab location if srf_location == .
	drop if _merge == 2
	drop _merge

	merge m:1 location year using "Data/02. Peacekeeping/UNPO5_population_for_adjustment.dta", update
	*tab year if _merge == 1 						 /*1948-1959, 2019*/
	*tab MissionAbbrev if _merge == 1 & year != 2019 /*UNEF I, UNMOGIP, UNOGIL, and UNTSO*/
	drop if _merge == 2
	drop _merge
/*
	sort MissionAbbrev ym
	foreach var of varlist MilitaryBdg-Nbd {
		replace    srf_`var' = `var'/srf_location    if    srf_`var' == .
		replace ln_srf_`var' = `var'/ln_srf_location if ln_srf_`var' == .
		replace    pop_`var' = `var'/pop_location    if    pop_`var' == .
		replace ln_pop_`var' = `var'/ln_pop_location if ln_pop_`var' == .
	} /*Nothing happens ...*/
*/


/*(d) Conflict-month data*/
	*tab MissionAbbrev if UCDPNewID == "" /*Missions that are not deployed to any conflicts: MINURCA, ONUC, UNMIT, UNPREDEP, UNSF.*/
	drop if UCDPNewID == ""

	/*Mission-conflict-month data:
	  In case multiple conflicts hosted a mission, multiple obs will be created.*/
	split UCDPNewID, p(", ") gen(conflict_id_)
	gen expand = .
	local i = 5
	count if expand == .
	while r(N) > 0 {
		recode expand (.=`i') if conflict_id_`i' != ""
		local i = `i' - 1
		count if expand == .
	}
	expand expand

	bysort MissionAbbrev ym: gen x = _n
	gen conflict_id = .
	foreach num of numlist 1/5 {
		replace conflict_id = real(conflict_id_`num') if x == `num'
	}
	drop UCDPNewID conflict_id_* expand x
	drop if conflict_id == 385 /*385 == 390; 385 changed to 390 because of the Croatian independence.*/

	/*Conflict-month data:
	  In case a conflict hosted multiple missions at the same time, their expenditures will be aggregated.
	  However, in some cases (mostly around the month when a mission was replaced with another),
	  there are missing values of financial resources for at least one of missions. To aggregate
	  the only non-missing values, I drop observations with missing values like below.*/
	sort conflict_id ym MissionAbbrev
	by conflict_id ym: gen x = _n
	*tab x /*max = 3 missions at the same time*/
	by conflict_id ym: egen max_x = max(x)
	*br if max_x >= 2 /*To drop (unnecessary) missing values*/
	drop if ///
		(conflict_id == 218 & x == 2)                                      | /*UNIPOM, UNMOGIP*/      ///
		(conflict_id == 300 & x == 2)                                      | /*UNAMIC, UNTAC*/        ///
		(conflict_id == 327 & ym == 376 & x == 2)                          | /*UNAVEM I, II*/         ///
		(conflict_id == 330 & x == 1 & max_x == 2)                         | /*UNMISET, UNTAET*/      ///
		(conflict_id == 337 & x == 2)                                      | /*UNOSOM I, II*/         ///
		(conflict_id == 374 & ym >= 408 & x == 2)                          | /*UNAMIR, UNOMUR*/       ///
		(conflict_id == 381 & x == 2 & (MissionAbbrev == "UNSMIH" | MissionAbbrev == "UNTMIH")) | /*MIPONUH, UNSMIH / MIPONUH, UNTMIH*/ ///
		(conflict_id == 382 & x == 2)                                      | /*UNOMSIL, UNAMSIL*/     ///
		(conflict_id == 389 & ym == 431 & x == 1 & max_x == 2)             | /*UNMIBH, UNPROFOR*/     ///
		(conflict_id == 390 & x == 1 & max_x >= 2)                         | /*UNCRO, UNPROFOR, UNTAES, UNMOP, UNPSG*/ ///
		(conflict_id == 390 & ym >= 456 & ym <= 461 & x == 2 & max_x == 3) | /*UNTAES, UNMOP, UNPSG*/ ///
		(conflict_id == 397 & ym == 431 & x == 1 & max_x == 2)             | /*UNMIBH, UNPROFOR*/     ///
		(conflict_id == 398 & ym == 431 & x == 1 & max_x == 2)               /*UNMIBH, UNPROFOR*/

	/*When multiple missions have non-missing expenditures at the same time:
	  I aggregate the expenditures of multiple missions for a given conflict-month.
	  The missions separately work for the same conflict (their expenditures should not be overlapped).
	   - 309          : UNMIS and UNAMID
	   - 327          : UNAVEM I, II, and III
	   - 374          : UNAMIR and UNOMUR
	   - 381          : MINUSTAH and MINUJUSTH
	   - 389          : UNPROFOR and UNMIBH
	   - 390, 397, 398: UNPROFOR and UNTAES
	   - 419          : MINUCI and UNOCI*/
	foreach var of varlist MilitaryBdg-Nbd srf_MilitaryBdg-ln_pop_Nbd {
		by conflict_id ym: egen missing = sum(missing(`var'))
		by conflict_id ym: egen sum_`var' = sum(`var') if missing == 0
		replace `var' = sum_`var' if missing == 0
		drop missing sum_`var'
	}
	duplicates drop conflict_id ym, force
	drop x max_x Mission*Abbrev Country*Name Territory*Name location-ln_pop_location


/*(e) Others*/
	rename *, lower
	order conflict_id ym year month, first
	label variable conflict_id    "Conlict id"
	label variable ym             "Year-month"
	label variable year           "Year"
	label variable month          "Month"
	label variable militarybdg    "Budget for the military component"
	label variable militaryexp    "Expenditure for the military component"
	label variable militaryvar    "Variance for the military component"
	label variable policebdg      "Budget for the police component"
	label variable policeexp      "Expenditure for the police component"
	label variable policevar      "Variance for the police component"
	label variable civilianbdg    "Budget for the civilian component"
	label variable civilianexp    "Expenditure for the civilian component"
	label variable civilianvar    "Variance for the civilian component"
	label variable opsbdg         "Budget for the operational cost"
	label variable opsexp         "Expenditure for the operational cost"
	label variable opsvar         "Variance for the operational cost"
	label variable grossbdg       "Budget for gross requirements"
	label variable grossexp       "Expenditure for gross requirements"
	label variable grossvar       "Variance for gross requirements"
	label variable netbdg         "Budget for net requirements"
	label variable netexp         "Expenditure for net requirements"
	label variable netvar         "Variance for net requirements"
	label variable totalbdg       "Budget for total requirements"
	label variable totalexp       "Expenditure for total requirements"
	label variable totalvar       "Variance for total requirements"
	label variable milobbdg       "Budget for military observers"
	label variable milobexp       "Expenditure for military observers"
	label variable milobvar       "Variance for military observers"
	label variable milconbdg      "Budget for military contingents"
	label variable milconexp      "Expenditure for military contingents"
	label variable milconvar      "Variance for military contingents"
	label variable milotherbdg    "Budget for other costs pertaining to military personnel"
	label variable milotherexp    "Expenditure for other costs pertaining to military personnel"
	label variable milothervar    "Variance for other costs pertaining to military personnel"
	label variable unpolbdg       "Budget for United Nations police"
	label variable unpolexp       "Expenditure for United Nations police"
	label variable unpolvar       "Variance for United Nations police"
	label variable fpubdg         "Budget for formed police units"
	label variable fpuexp         "Expenditure for formed police units"
	label variable fpuvar         "Variance for formed police units"
	label variable civintlbdg     "Budget for internaional civilian staff"
	label variable civintlexp     "Expenditure for internaional civilian staff"
	label variable civintlvar     "Variance for internaional civilian staff"
	label variable civnatbdg      "Budget for local civilian staff"
	label variable civnatexp      "Expenditure for local civilian staff"
	label variable civnatvar      "Variance for local civilian staff"
	label variable civstaffbdg    "Budget for internaional and local civilian staff"
	label variable civstaffexp    "Expenditure for internaional and local civilian staff"
	label variable civstaffvar    "Variance for internaional and local civilian staff"
	label variable intlconbdg     "Budget for internaional contractual personnel"
	label variable intlconexp     "Expenditure for internaional contractual personnel"
	label variable intlconvar     "Variance for internaional contractual personnel"
	label variable consulbdg      "Budget for consultants"
	label variable consulexp      "Expenditure for consultants"
	label variable consulvar      "Variance for consultants"
	label variable unvbdg         "Budget for United Nations volunteers"
	label variable unvexp         "Expenditure for United Nations volunteers"
	label variable unvvar         "Variance for United Nations volunteers"
	label variable temporarybdg   "Budget for general temporary personnel"
	label variable temporaryexp   "Expenditure for general temporary personnel"
	label variable temporaryvar   "Variance for general temporary personnel"
	label variable commonbdg      "Budget for common staff"
	label variable commonexp      "Expenditure for common staff"
	label variable commonvar      "Variance for internaional civilian staff"
	label variable govtbdg        "Budget for government-provided personnel"
	label variable govtexp        "Expenditure for government-provided personnel"
	label variable govtvar        "Variance for government-provided personnel"
	label variable electionbdg    "Budget for civilian electoral observers"
	label variable electionexp    "Expenditure for civilian electoral observers"
	label variable electionvar    "Variance for civilian electoral observers"
	label variable deathbdg       "Budget for death and disability awards"
	label variable deathexp       "Expenditure for death and disability awards"
	label variable deathvar       "Variance for death and disability awards"
	label variable otherprogbdg   "Budget for other programmes"
	label variable otherprogexp   "Expenditure for other programmes"
	label variable otherprogvar   "Variance for other programmes"
	label variable unlbbdg        "Budget for UNLB/IMIS"
	label variable unlbexp        "Expenditure for UNLB/IMIS"
	label variable unlbvar        "Variance for UNLB/IMIS"
	label variable supportbdg     "Budget for support account for PKOs"
	label variable supportexp     "Expenditure for support account for PKOs"
	label variable supportvar     "Variance for support account for PKOs"
	label variable staffassessbdg "Budget for staff assessment"
	label variable staffassessexp "Expenditure for staff assessment"
	label variable staffassessvar "Variance for staff assessment"
	label variable otherincomebdg "Budget for other income"
	label variable otherincomeexp "Expenditure for other income"
	label variable otherincomevar "Variance for other income"
	label variable volconnbdbdg   "Budget for non-budgeted voluntary contributions"
	label variable volconnbdexp   "Expenditure for non-budgeted voluntary contributions"
	label variable volconnbdvar   "Variance for non-budgeted voluntary contributions"
	label variable volconbdbdg    "Budget for budgeted voluntary contributions"
	label variable volconbdexp    "Expenditure for budgeted voluntary contributions"
	label variable volconbdvar    "Variance for budgeted voluntary contributions"
	label variable volconbdg      "Budget for total amount of voluntary contributions"
	label variable volconexp      "Expenditure for total amount of voluntary contributions"
	label variable volconvar      "Variance for total amount of voluntary contributions"
	label variable other          "Total amount of other revenue and adjustments"
	label variable majtotal       "Total amount of major COE equipment"
	label variable selftotal      "Total amount of self-sustainment"
	label variable nbd            "Total amount of non-budgeted contributions"

	notes militaryexp: "Military observers + military contingents + other costs pertaining to military personnel"
	notes policeexp  : "United Nations police + formed police units"
	notes civilianexp: "International civilian staff + local civilian staff + United Nations volunteers + general temporary assistance + government-provided personnel + civilian electoral observers + international contractual personnel + consultants + staff assessment"
	save "Data/02. Peacekeeping/UNPO6_financial_resources_conflict-month.dta", replace
	/*Some types of variables regarding peacekeeping financial resources are included
	  in this dataset (and other datasets constructed below) even though they are not used for the analysis.
	   -    srf_*: [financial resources] / surface area size
	   - ln_srf_*: [financial resources] / log(surface area size)
	   -    pop_*: [financial resources] / population size
	   - ln_pop_*: [financial resources] / log(population size)*/



*------------------------------------------------------------------------------*
/*(7) UN PKO personnel                                                        */
*------------------------------------------------------------------------------*
/*(7-1) Mission-month data (raw data + adjusted)                              */
*------------------------------------------------------------------------------*
/*The time unit of data on the # of peacekeepers is already originally a month in the raw data,
  unlike the financial resources data. As with the literature, I use the # of peacekeepers
  at the monthly data point: the values may be the daily average per month, but it's not clear.*/

	import excel using "RawData/02. Peacekeeping/PKOP_v1.xlsx", firstrow clear
	drop if Average == 1


/*(a) Missing values*/
	/*Replace NA with .*/
	gen flag = 0
	replace flag = 1 if ///
		Milob   == "NA" | Milcon  == "NA"  | Military   == "NA" | Police       == "NA"  | ///
		Civintl == "NA" | Civintl == "N/A" | Civnat     == "NA" | Civnat       == "N/A" | ///
		UNV     == "NA" | OAUob   == "NA"  | Officernat == "NA" | Officerstaff == "NA"
	replace flag = 2 if Missing == 0 & Military != "." & Milob == "." & Milcon == "." /*For these cases, Milob and Milcon should not be replaced with 0s.*/
	foreach var of varlist Milob Milcon Police-Officerstaff {
		replace `var' = "0" if flag == 1 & `var' == "."
		replace `var' = "." if flag == 1 & (`var' == "NA" | `var' == "N/A")
	}
	replace Military = "." if flag == 1 & Military == "NA"

	/*Replace . with 0*/
	foreach var of varlist Milob Milcon Police-Officerstaff {
		replace `var' = "0" if `var' == "." & flag == 0 & Missing == 0
	}
	destring, replace
	replace Military = Milob + Milcon if Milob != . & Milcon != . & Military == .

	/*Replace . with the previous month (August 2004):
	  The background note that I referred to code was published also regarding August 2004,
	  but the PDF file is the same as for July 2004, and then I only coded missing values.
	  However, to reduce missing values, here I replace the missing values.*/
	foreach var of varlist Milob-Officerstaff {
		replace `var' = `var'[_n-1] if Year == 2004 & Month == 8
	}
	recode Missing (1=0) if Year == 2004 & Month == 8
	drop flag Document* Day Note* Missing Average Fatality


/*(b) Make variables*/
	gen ym = ym(Year, Month)
	rename Year Month, lower /*same as financial resources data!*/

	*count             if Officerstaff != 0 & Officerstaff != . /*305 obs have nonzero staff officers.*/
	*tab MissionAbbrev if Officerstaff != 0 & Officerstaff != .
	*tab year month    if Officerstaff != 0 & Officerstaff != . /*reported only after May 2017*/
	*twoway line Milcon ym if MissionAbbrev == "UNFICYP" || line Officerstaff ym if MissionAbbrev == "UNFICYP", xline(688) /*example; staff officers seems to have been reported together with contingents before Apr. 2017.*/
	gen Military2 = Milob + Milcon + Officerstaff /*consistent over time*/

	*count             if (OAUob != 0 & OAUob != .) | (Officernat != 0 & Officernat != .) /*113 obs*/
	*tab MissionAbbrev if (OAUob != 0 & OAUob != .) | (Officernat != 0 & Officernat != .) /*only three missions: MINURSO, UNMIBH, and UNMIK*/
	*tab year month    if (OAUob != 0 & OAUob != .) | (Officernat != 0 & Officernat != .) /*reported only until Jun. 2001; unclear if these subcategories were originally included in local staff.*/
	gen Civilian  = Civintl + Civnat + UNV
	gen Civilian2 = Civintl + Civnat + UNV + OAUob + Officernat


/*(c) Adjustment factors (surface area / population)*/
	/*For merge*/
	gen location = CountryName
	replace location = TerritoryName if TerritoryName != ""
	replace location = "CAR and Chad"          if MissionAbbrev == "MINURCAT"
	replace location = "Central America"       if MissionAbbrev == "ONUCA"
	replace location = "Eastern Slavonia, Baranja and Western Sirmium" if MissionAbbrev == "UNTAES" | MissionAbbrev == "UNPSG"
	replace location = "Ethiopia and Eritrea"  if MissionAbbrev == "UNMEE"
	replace location = "India and Pakistan"    if MissionAbbrev == "UNMOGIP"
	replace location = "Iran and Iraq"         if MissionAbbrev == "UNIIMOG"
	replace location = "Iraq and Kuwait"       if MissionAbbrev == "UNIKOM"
	replace location = "Uganda-Rwanda"         if MissionAbbrev == "UNOMUR"
	replace location = "Rwanda; Uganda-Rwanda" if MissionAbbrev == "UNAMIR" & Mission2Abbrev == "UNOMUR"
	replace location = "Yugoslavia"            if MissionAbbrev == "UNPROFOR"

	/*Surface areas*/
	merge m:1 location using "Data/02. Peacekeeping/UNPO4_surface_areas_for_adjustment.dta"
	*tab MissionAbbrev if _merge == 1 /*UNTSO*/
	*tab location if _merge == 2
	drop if _merge == 2
	drop _merge

	/*Population*/
	merge m:1 location year using "Data/02. Peacekeeping/UNPO5_population_for_adjustment.dta"
	*tab year if _merge == 1                         /*many obs for 2019*/
	*tab MissionAbbrev if _merge == 1 & year <= 2018 /*UNTSO*/
	drop if _merge == 2
	drop _merge

	sort location ym
	foreach var of varlist Milob-Officerstaff Military2-Civilian2 {
		gen    srf_`var' = `var'/srf_location
		gen ln_srf_`var' = `var'/ln_srf_location
		gen    pop_`var' = `var'/pop_location
		gen ln_pop_`var' = `var'/ln_pop_location
	}


/*(d) Others*/
	order MissionAbbrev-month ym Military* Police Civilian* Milob Milcon Officerstaff                    ///
		Civintl Civnat UNV OAUob Officernat Preparation Withdrawal location srf_location-ln_pop_location ///
		srf_Military-ln_pop_Military srf_Military2-ln_pop_Military2 srf_Police-ln_pop_Police srf_Civilian-ln_pop_Civilian2 ///
		srf_Milob-ln_pop_Milcon srf_Officerstaff-ln_pop_Officerstaff, first
	foreach var of varlist * {
		label variable `var' ""
	}
	save "Data/02. Peacekeeping/UNPO7_personnel_base.dta", replace



*------------------------------------------------------------------------------*
/*(7-2) Mission-month data                                                    */
*------------------------------------------------------------------------------*
/*(a) Mission-month data*/
	use "Data/02. Peacekeeping/UNPO1_mission_list.dta", clear
	keep if MissionType == "PKO"
	drop MissionType

	gen expand = ym(EndYear, EndMonth) - ym(StartYear, StartMonth) + 1 if Ongoing == 0
	replace expand = ym(2019,12) - ym(StartYear, StartMonth) + 1 if Ongoing == 1
	expand expand

	bysort MissionAbbrev: gen x = _n
	gen ym = ym(StartYear, StartMonth) if x == 1
	by MissionAbbrev: replace ym = ym[_n-1] + 1 if x != 1

	gen month = StartMonth if x == 1
	replace month = month[_n-1] + 1 if x != 1
	count if month > 12
	while r(N) > 0 {
		replace month = month - 12 if month > 12
		count if month > 12
	}

	gen year = StartYear if x == 1
	replace year = year[_n-1] if x != 1 & month[_n-1] != 12
	count if year == .
	while r(N) > 0 {
		by MissionAbbrev: replace year = year[_n-1] + 1 if month[_n-1] == 12
		by MissionAbbrev: replace year = year[_n-1] if year == . & month[_n-1] != 12
		count if year == .
	}
	drop expand x StartYear-EndDay ChapterVII Ongoing


/*(b) Merge mission-month data of # of peacekeepers*/
	merge 1:1 MissionAbbrev year month ym using "Data/02. Peacekeeping/UNPO7_personnel_base.dta"
	*tab year if _merge == 1
	*tab MissionAbbrev if _merge == 1 & year >= 1989

	sort MissionAbbrev ym
	*by MissionAbbrev: egen min_year = min(year)
	*tab MissionAbbrev _merge if min_year <= 1988                                  /*Among the missions starting before 1988, the only UNIIMOG is included in the PKOP dataset for all of its periods.*/
	*tab MissionAbbrev        if min_year >= 1989 & _merge == 1 & ym <= ym(2019,9) /*Among the missions starting after 1989, UNASOG is not included in the PKOP dataset. Other _merge == 1 missions (UNCRO, UNMOP, UNOMUR, and UNPREDEP) are reported together with other missions.*/
	*tab Mission2Abbrev
	*tab Mission3Abbrev
	*tab MissionAbbrev        if min_year >= 1989 & _merge == 2 & Preparation == 0 & Withdrawal == 0 /*Some periods (ym == 432 to 449) of UNPROFOR are not included in the mission-month panel data because # of personnel for other missions are reported in those obs.*/
	*tab MissionAbbrev        if _merge == 3 & Withdrawal == 1                     /*The first months of withrawal for MINURCA, MIPONUH, MONUA, ONUMOZ, UNAMIR, UNMIS, UNMOT, and UNTAES. Note that these missions already finished the operations in those months.*/
	*drop min_year
	/*Findings are ...
	   - _merge == 1: Some mission-months that should be _merge == 3 are in this because they are reported with other mission-moths (see Mission2Abbrev and Mission3Abbrev).
	   - _merge == 2: Preparation == 1 | Withdrawal == 1 | (MissionAbbrev == "UNPROFOR" & ym >= 432 & ym <= 449); but note that 8 withdrawal cases are _merge == 3.
	   - _merge == 3: 8 mission-months after withdrawal are also in this.*/

	by MissionAbbrev: replace UCDPNewID = UCDPNewID[_n-1] if UCDPNewID == "" & _merge == 2
	foreach var of varlist CountryName-Territory3Name location-ln_srf_location {
		gen missing = missing(`var')
		by MissionAbbrev: replace `var' = `var'[_n-1] if missing == 1 & _merge == 1
		drop missing
	}
	by MissionAbbrev: replace    pop_location = pop_location[_n-1]    if    pop_location == . & _merge == 1 & year == year[_n-1]
	by MissionAbbrev: replace ln_pop_location = ln_pop_location[_n-1] if ln_pop_location == . & _merge == 1 & year == year[_n-1]

	gsort MissionAbbrev -ym
	by MissionAbbrev: replace UCDPNewID = UCDPNewID[_n-1] if UCDPNewID == "" & _merge == 2
	foreach var of varlist CountryName-Territory3Name location-ln_srf_location {
		gen missing = missing(`var')
		by MissionAbbrev: replace `var' = `var'[_n-1] if missing == 1 & _merge == 1
		drop missing
	}
	by MissionAbbrev: replace    pop_location =    pop_location[_n-1] if    pop_location == . & _merge == 1 & year == year[_n-1]
	by MissionAbbrev: replace ln_pop_location = ln_pop_location[_n-1] if ln_pop_location == . & _merge == 1 & year == year[_n-1]
	drop _merge Preparation Withdrawal


/*(c) Put values for missing data (just to complete ...)*/
	replace CountryName = "Dominican Republic"    if MissionAbbrev == "DOMREP"
	replace CountryName = "Congo"                 if MissionAbbrev == "ONUC"
	replace CountryName = "Chad"                  if MissionAbbrev == "UNASOG"
	replace CountryName = "Croatia"               if MissionAbbrev == "UNCRO"
	replace CountryName = "Egypt, Israel"         if MissionAbbrev == "UNEF I" | MissionAbbrev == "UNEF II"
	replace CountryName = "Afghanistan, Pakistan" if MissionAbbrev == "UNGOMAP"
	replace CountryName = "India, Pakistan"       if MissionAbbrev == "UNIPOM"
	replace CountryName = "Croatia"               if MissionAbbrev == "UNMOP"
	replace CountryName = "Lebanon"               if MissionAbbrev == "UNOGIL"
	replace CountryName = "Indonesia"             if MissionAbbrev == "UNSF"
	replace CountryName = "Yemen"                 if MissionAbbrev == "UNYOM"

	replace TerritoryName = "Aouzou Strip"    if MissionAbbrev == "UNASOG"
	replace TerritoryName = "Suez"            if MissionAbbrev == "UNEF I"
	replace TerritoryName = "Suez; Sinai"     if MissionAbbrev == "UNEF II"
	replace TerritoryName = "Prevlaka"        if MissionAbbrev == "UNMOP"
	replace TerritoryName = "West New Guinea" if MissionAbbrev == "UNSF"

	replace location = CountryName if location == ""
	replace location = "Afghanistan and Pakistan" if MissionAbbrev == "UNGOMAP"
	replace location = "India and Pakistan"       if MissionAbbrev == "UNIPOM"
	replace location = TerritoryName if MissionAbbrev == "UNASOG" | MissionAbbrev == "UNEF I" | MissionAbbrev == "UNEF II" | MissionAbbrev == "UNMOP" | MissionAbbrev == "UNSF"

	merge m:1 location using "Data/02. Peacekeeping/UNPO4_surface_areas_for_adjustment.dta", update
	*tab location if _merge == 2
	*tab location if srf_location == .
	*tab MissionAbbrev if _merge == 1 /*UNTSO*/
	drop if _merge == 2
	drop _merge

	merge m:1 location year using "Data/02. Peacekeeping/UNPO5_population_for_adjustment.dta", update
	*tab year if _merge == 1                         /*1948-1959, 2019*/
	*tab MissionAbbrev if _merge == 1 & year != 2019 /*UNEF I, UNMOGIP, UNOGIL, and UNTSO*/
	drop if _merge == 2
	drop _merge
/*
	sort MissionAbbrev ym
	foreach var of varlist Military-Officernat {
		replace    srf_`var' = `var'/srf_location    if    srf_`var' == .
		replace ln_srf_`var' = `var'/ln_srf_location if ln_srf_`var' == .
		replace    pop_`var' = `var'/pop_location    if    pop_`var' == .
		replace ln_pop_`var' = `var'/ln_pop_location if ln_pop_`var' == .
	}
*/


/*(d) Conflict-month data*/
	*tab MissionAbbrev if UCDPNewID == "" /*MINURCA, ONUC, UNMIT, UNPREDEP, UNSF*/
	drop if UCDPNewID == ""

	/*Mission-conflict-month data*/
	split UCDPNewID, p(", ") gen(conflict_id_)
	gen expand = .
	local i = 5
	count if expand == .
	while r(N) > 0 {
		recode expand (.=`i') if conflict_id_`i' != ""
		local i = `i' - 1
		count if expand == .
	}
	expand expand

	bysort MissionAbbrev ym: gen x = _n
	gen conflict_id = .
	foreach num of numlist 1/5 {
		replace conflict_id = real(conflict_id_`num') if x == `num'
	}
	drop UCDPNewID conflict_id_* expand x
	drop if conflict_id == 385 /*385 == 390; 385 changed to 390 because of the Croatian independence.*/

	/*Conflict-month data -> See financial resources dataset construction for more details*/
	sort conflict_id ym MissionAbbrev
	by conflict_id ym: gen x = _n
	*tab x /*max = 3*/
	by conflict_id ym: egen max_x = max(x)
	*br if max_x >= 2
	drop if ///
		(conflict_id == 218 & x == 2)                          | /*UNIPOM, UNMOGIP*/      ///
		(conflict_id == 300 & x == 2)                          | /*UNAMIC, UNTAC*/        ///
		(conflict_id == 309 & ym == 570 & x == 1 & max_x == 2) | /*UNMIS, UNAMID*/        ///
		(conflict_id == 327 & ym == 376 & x == 2)              | /*UNAVEM I, II*/         ///
		(conflict_id == 327 & ym == 421 & x == 1 & max_x == 2) | /*UNAVEM II, III*/       ///
		(conflict_id == 330 & x == 2)                          | /*UNMISET, UNTAET*/      ///
		(conflict_id == 337 & x == 2)                          | /*UNOSOM I, II*/         ///
		(conflict_id == 374 & ym >= 408 & x == 2)              | /*UNAMIR, UNOMUR*/       ///
		(conflict_id == 381 & x == 2)                          | /*MINUSTAH, MINUJUSTH*/  ///
		(conflict_id == 382 & x == 2)                          | /*UNOMSIL, UNAMSIL*/     ///
		(conflict_id == 389 & ym == 431 & x == 1 & max_x == 2) | /*UNMIBH, UNPROFOR*/     ///
		(conflict_id == 390 & x == 1 & max_x >= 2)             | /*UNCRO, UNPROFOR, UNTAES, UNMOP, UNPSG*/ ///
		(conflict_id == 397 & ym == 431 & x == 1 & max_x == 2) | /*UNMIBH, UNPROFOR*/     ///
		(conflict_id == 398 & ym == 431 & x == 1 & max_x == 2) | /*UNMIBH, UNPROFOR*/     ///
		(conflict_id == 419 & x == 1 & max_x == 2)               /*MINUCI, UNOCI*/

	/*When multiple missions have non-missing personnel at the same time:
	   - 309          : UNMIS and UNAMID
	   - 374          : UNAMIR and UNOMUR
	   - 390          : UNTAES, UNMOP, and UNPSG
	   - 389, 397, 398: UNMIBH and UNPROFOR*/
	foreach var of varlist Military-Officernat srf_Military-ln_pop_Officernat {
		by conflict_id ym: egen missing = sum(missing(`var'))
		by conflict_id ym: egen sum_`var' = sum(`var') if missing == 0
		replace `var' = sum_`var' if missing == 0
		drop missing sum_`var'
	}
	duplicates drop conflict_id ym, force
	drop x max_x Mission*Abbrev Country*Name Territory*Name location-ln_pop_location


/*(e) Others*/
	rename *, lower
	order conflict_id ym year month, first
	label variable conflict_id  "Conflict id"
	label variable ym           "Year-month"
	label variable year         "Year"
	label variable month        "Month"
	label variable military     "# of military peacekeepers (excl. staff officers after May 2017)"
	label variable military2    "# of military peacekeepers"
	label variable police       "# of police peacekeepers"
	label variable civilian     "# of civilian peacekeepers"
	label variable civilian2    "# of civilian peacekeepers (incl. OAU observers/national officers)"
	label variable milob        "# of military observers"
	label variable milcon       "# of military contingents"
	label variable officerstaff "# of staff officers"
	label variable civintl      "# of international civilin staff"
	label variable civnat       "# of local civilian staff"
	label variable unv          "# of United Nations volunteers"
	label variable oauob        "# of OAU observers"
	label variable officernat   "# of national officers"
	save "Data/02. Peacekeeping/UNPO7_personnel_conflict-month.dta", replace



*------------------------------------------------------------------------------*
/*(8) Merge all UN PKO data!                                                  */
*------------------------------------------------------------------------------*
	use "Data/02. Peacekeeping/UNPO2_pko_conflict-month.dta", clear

/*(a) Financial resources data*/
	merge 1:1 conflict_id ym year month using "Data/02. Peacekeeping/UNPO6_financial_resources_conflict-month.dta"
	*tab year if _merge == 1 /*Only 2019*/
	*tab pko1 if _merge == 1 /*Note that only MINUSMA was deployed to two conflicts.*/
	*tab conflict_id if _merge == 2
	*count if pko1 == ""     /*170 obs (_merge == 2) have financial resources data though missions were not operated.*/
	recode pko pko_robust (.=0) if _merge == 2
	rename _merge _merge_f

/*(b) Personnel data*/
	merge 1:1 conflict_id ym year month using "Data/02. Peacekeeping/UNPO7_personnel_conflict-month.dta"
	*tab year if _merge == 1
	*tab pko1 if _merge == 1 /*No names = _merge == 2 in (a)*/
	*tab conflict_id if _merge == 2
	recode pko pko_robust (.=0) if _merge == 2

	rename _merge _merge_p
	*tab _merge_f _merge_p, m
	drop _merge*

	sort conflict_id ym
	save "Data/02. Peacekeeping/UNPO_datasets.dta", replace





********************************************************************************
********************************************************************************
********************************************************************************

/*Other control variables*/

/*List of datasets that will be constructed and merged in this section:
  - (1) Country list        : Sample definition
  - (2) World Bank Open Data: Control variables (ODA, military personnel, GDP, population, and others)
  - (3) V-Dem               : Control variable  (democracy)
  - (4) MILINDA             : Control variables (non-UN PKOs)*/



*------------------------------------------------------------------------------*
/*(1) Country list                                                            */
*------------------------------------------------------------------------------*
/*(a) States*/
	import delimited using "RawData/03. Controls/GW ccodes/iisystem.dat", clear
	keep v1 v3
	rename v1 GWccode
	rename v3 location
	duplicates drop GWccode location, force
	drop if location == "Madagascar (Malagasy)"

	*replace location = "Württemberg"   if GWccode == 271
	replace location = "Italy"         if GWccode == 325
	replace location = "Russia"        if GWccode == 365
	replace location = "Belarus"       if GWccode == 370
	replace location = "Côte d'Ivoire" if GWccode == 437
	replace location = "Burkina Faso"  if GWccode == 439
	replace location = "Congo, Democratic Republic of" if GWccode == 490
	replace location = "Tanzania"      if GWccode == 510
	replace location = "Zimbabwe"      if GWccode == 552
	replace location = "Iran"          if GWccode == 630
	replace location = "Turkey"        if GWccode == 640
	replace location = "Yemen"         if GWccode == 678
	replace location = "Myanmar"       if GWccode == 775
	replace location = "Sri Lanka"     if GWccode == 780
	replace location = "Cambodia"      if GWccode == 811
	replace location = "Vietnam"       if GWccode == 815
	save "Data/03. Controls/Ctrl1_GWccodes_states.dta", replace


/*(b) Micro states*/
	import delimited using "RawData/03. Controls/GW ccodes/microstatessystem.dat", clear
	keep v1 v3
	rename v1 GWccode
	rename v3 location
	duplicates drop GWccode location, force

	*replace location = "São Tomé and Principe" if GWccode == 403
	replace location = "Samoa"                 if GWccode == 990
	save "Data/03. Controls/Ctrl1_GWccodes_microstates.dta", replace


/*(c) Append*/
	use "Data/03. Controls/Ctrl1_GWccodes_states.dta", clear
	append using "Data/03. Controls/Ctrl1_GWccodes_microstates.dta"
	sort GWccode
	save "Data/03. Controls/Ctrl1_GWccodes.dta", replace



*------------------------------------------------------------------------------*
/*(2) World Bank Open Data                                                    */
*------------------------------------------------------------------------------*
/*(a) Make variables*/
	/*Surface area (sq. km)*/
	import excel using "RawData/03. Controls/World Bank/API_AG.SRF.TOTL.K2_DS2_en_excel_v2_723239.xls", sheet("Data") clear
	local i = 1960
	foreach var of varlist E-BL {
		rename `var' srf`i'
		local i = `i' + 1
	}
	rename A location
	drop if _n <= 4
	drop B-D
	reshape long srf, i(location) j(year)
	destring, replace
	save "Data/03. Controls/Ctrl2_surface_areas.dta", replace

	/*Net official development assistance (ODA; current US$)*/
	import excel using "RawData/03. Controls/World Bank/API_DT.ODA.ODAT.CD_DS2_en_excel_v2_712989.xls", sheet("Data") clear
	local i = 1960
	foreach var of varlist E-BL {
		rename `var' oda`i'
		local i = `i' + 1
	}
	rename A location
	drop if _n <= 4
	drop B-D
	reshape long oda, i(location) j(year)
	destring, replace
	save "Data/03. Controls/Ctrl2_oda.dta", replace

	/*Military personnel*/
	import excel using "RawData/03. Controls/World Bank/API_MS.MIL.TOTL.P1_DS2_en_excel_v2_715393.xls", sheet("Data") clear
	local i = 1960
	foreach var of varlist E-BL {
		rename `var' milper`i'
		local i = `i' + 1
	}
	rename A location
	drop if _n <= 4
	drop B-D
	reshape long milper, i(location) j(year)
	destring, replace
	save "Data/03. Controls/Ctrl2_military_personnel.dta", replace

	/*Military expenditure (current US$)*/
	import excel using "RawData/03. Controls/World Bank/API_MS.MIL.XPND.CD_DS2_en_excel_v2_713202.xls", sheet("Data") clear
	local i = 1960
	foreach var of varlist E-BL {
		rename `var' milexp`i'
		local i = `i' + 1
	}
	rename A location
	drop if _n <= 4
	drop B-D
	reshape long milexp, i(location) j(year)
	destring, replace
	save "Data/03. Controls/Ctrl2_military_expenditures.dta", replace

	/*Gross domestic product (GDP; current US$)*/
	import excel using "RawData/03. Controls/World Bank/API_NY.GDP.MKTP.CD_DS2_en_excel_v2_739713.xls", sheet("Data") clear
	local i = 1960
	foreach var of varlist E-BL {
		rename `var' gdp`i'
		local i = `i' + 1
	}
	rename A location
	drop if _n <= 4
	drop B-D
	reshape long gdp, i(location) j(year)
	destring, replace
	save "Data/03. Controls/Ctrl2_gdp.dta", replace

	/*Population*/
	import excel using "RawData/03. Controls/World Bank/API_SP.POP.TOTL_DS2_en_excel_v2_713231.xls", sheet("Data") clear
	local i = 1960
	foreach var of varlist E-BL {
		rename `var' pop`i'
		local i = `i' + 1
	}
	rename A location
	drop if _n <= 4
	drop B-D
	reshape long pop, i(location) j(year)
	destring, replace
	save "Data/03. Controls/Ctrl2_population.dta", replace

	/*Merge!*/
	use "Data/03. Controls/Ctrl2_surface_areas.dta", clear
	foreach x in oda military_personnel military_expenditures gdp population {
		merge 1:1 location year using "Data/03. Controls/Ctrl2_`x'.dta"
		drop _merge
	}


/*(b) Merge GW country codes*/
	*tab location
	replace location = "Antigua & Barbuda"                       if location == "Antigua and Barbuda"
	replace location = "Bahamas"                                 if location == "Bahamas, The"
	replace location = "Bosnia-Herzegovina"                      if location == "Bosnia and Herzegovina"
	replace location = "Brunei"                                  if location == "Brunei Darussalam"
	replace location = "Cape Verde"                              if location == "Cabo Verde"
	replace location = "Congo, Democratic Republic of"           if location == "Congo, Dem. Rep."
	replace location = "Congo"                                   if location == "Congo, Rep."
	replace location = "Côte d'Ivoire"                           if location == "Cote d'Ivoire"
	replace location = "Egypt"                                   if location == "Egypt, Arab Rep."
	replace location = "Swaziland"                               if location == "Eswatini"
	replace location = "Gambia"                                  if location == "Gambia, The"
	replace location = "German Federal Republic"                 if location == "Germany"
	replace location = "Iran"                                    if location == "Iran, Islamic Rep."
	replace location = "Korea, People's Republic of"             if location == "Korea, Dem. People’s Rep."
	replace location = "Korea, Republic of"                      if location == "Korea, Rep."
	replace location = "Laos"                                    if location == "Lao PDR"
	replace location = "Federated States of Micronesia"          if location == "Micronesia, Fed. Sts."
	replace location = "Macedonia (Former Yugoslav Republic of)" if location == "North Macedonia"
	replace location = "Rumania"                                 if location == "Romania"
	replace location = "Russia"                                  if location == "Russian Federation"
	replace location = "São Tomé and Principe"                   if location == "Sao Tome and Principe"
	replace location = "Slovakia"                                if location == "Slovak Republic"
	replace location = "Saint Kitts and Nevis"                   if location == "St. Kitts and Nevis"
	replace location = "Saint Lucia"                             if location == "St. Lucia"
	replace location = "Saint Vincent and the Grenadines"        if location == "St. Vincent and the Grenadines"
	replace location = "Surinam"                                 if location == "Suriname"
	replace location = "Syria"                                   if location == "Syrian Arab Republic"
	replace location = "East Timor"                              if location == "Timor-Leste"
	replace location = "United States of America"                if location == "United States"
	replace location = "Venezuela"                               if location == "Venezuela, RB"
	replace location = "Yemen"                                   if location == "Yemen, Rep."

	merge m:1 location using "Data/03. Controls/Ctrl1_GWccodes.dta"
	*tab location if _merge == 1
	/*Countries only World Bank data includes:
	  American Samoa, Aruba, Bermuda, British Virgin Islands, Cayman Islands,
	  Channel Islands, Curacao, Faroe Islands, French Polynesia, Gibraltar,
	  Greenland, Guam, Hong Kong, Isle of Man, Macao, New Caledonia,
	  Northern Mariana Islands, Puerto Rico, Sint Martin (Dutch part),
	  St. Martin (French part), Turks and Caicos Islands, Virign Islands (U.S.),
	  West Bank and Gaza; and observations according to area and rank*/
	*tab location if _merge == 2 /*Old states or microstates.*/
	drop if _merge == 1 | _merge == 2
	drop _merge


/*(c) Data checks: annexation and independence/dissolution
	   - Annexation:
		 Annexed countries (i.e., German Democratic Republic and Yemen, People's Republic of)
		 are not coded in the World Bank data. Thus, the data for countries that annexed them need to be used.
	   - Independence/Dissolution:
		 Cannot clearly tell if the data for the former ruling country (e.g., Indonesia)
		 include the newly independent country (e.g., East Timor) before the independence.
		 However, mostly, independent countries have their own data even before the independence.
		 This implies that the World Bank data can be used to measure the features of the only government side
		 (e.g., Indonesia excluding the rebel side of East Timor). Sudan before independence of South Sudan as well,
		 but note that this specification differs from the measurement of surface area of operations for UNMIS.
		 Exception: Yugoslavia.*/
	sort GWccode year
/*
	/*[A1] Germany
		    - East and West were annexed on Oct. 3, 1990.
		    - German Democratic Republic is not included in the WB data.*/
	line srf    year if location == "German Federal Republic", xline(1990)
	line oda    year if location == "German Federal Republic", xline(1990)
	line milper year if location == "German Federal Republic", xline(1990)
	line milexp year if location == "German Federal Republic", xline(1990)
	line gdp    year if location == "German Federal Republic", xline(1990)
	line pop    year if location == "German Federal Republic", xline(1990)

	/*[A2] Yemen
			- North and South were annexed on May 21, 1990.
			- Yemen, People's Republic of is not included in the WB data.*/
	line    srf year if location == "Yemen", xline(1990)
	line    oda year if location == "Yemen", xline(1990)
	line milper year if location == "Yemen", xline(1990)
	line milexp year if location == "Yemen", xline(1990)
	line    gdp year if location == "Yemen", xline(1990)
	line    pop year if location == "Yemen", xline(1990)

	/*[I1] Indonesia
		    - East Timor became independent on May 20, 2002.
		    - East Timor has its own data even before the independence.*/
	twoway line srf    year if location == "Indonesia" || line srf    year if location == "East Timor", xline(2002)
	twoway line oda    year if location == "Indonesia" || line oda    year if location == "East Timor", xline(2002)
	twoway line milper year if location == "Indonesia" || line milper year if location == "East Timor", xline(2002)
	twoway line milexp year if location == "Indonesia" || line milexp year if location == "East Timor", xline(2002)
	twoway line gdp    year if location == "Indonesia" || line gdp    year if location == "East Timor", xline(2002)
	twoway line pop    year if location == "Indonesia" || line pop    year if location == "East Timor", xline(2002)

	/*[I2] Serbia
		    - Kosovo became independent on Feb. 17, 2008.
		    - Kosovo has its own data even before the independence.*/
	twoway line    srf year if location == "Serbia" || line    srf year if location == "Kosovo", xline(2008)
	twoway line    oda year if location == "Serbia" || line    oda year if location == "Kosovo", xline(2008)
	twoway line milper year if location == "Serbia" || line milper year if location == "Kosovo", xline(2008)
	twoway line milexp year if location == "Serbia" || line milexp year if location == "Kosovo", xline(2008)
	twoway line    gdp year if location == "Serbia" || line    gdp year if location == "Kosovo", xline(2008)
	twoway line    pop year if location == "Serbia" || line    pop year if location == "Kosovo", xline(2008)

	/*[I3] South Africa
			- Namibia became independent on Mar. 21, 1990.
			- Namibia has its own data even before the independence.*/
	twoway line    srf year if location == "South Africa" || line    srf year if location == "Namibia", xline(1990)
	twoway line    oda year if location == "South Africa" || line    oda year if location == "Namibia", xline(1990)
	twoway line milper year if location == "South Africa" || line milper year if location == "Namibia", xline(1990)
	twoway line milexp year if location == "South Africa" || line milexp year if location == "Namibia", xline(1990)
	twoway line    gdp year if location == "South Africa" || line    gdp year if location == "Namibia", xline(1990)
	twoway line    pop year if location == "South Africa" || line    pop year if location == "Namibia", xline(1990)

	/*[I4] Sudan
			- South Sudan became independent on Jul. 9, 2011.
			- South Sudan has its own data even before the independence.*/
	twoway line    srf year if location == "Sudan" || line    srf year if location == "South Sudan", xline(2011)
	twoway line    oda year if location == "Sudan" || line    oda year if location == "South Sudan", xline(2011)
	twoway line milper year if location == "Sudan" || line milper year if location == "South Sudan", xline(2011)
	twoway line milexp year if location == "Sudan" || line milexp year if location == "South Sudan", xline(2011)
	twoway line    gdp year if location == "Sudan" || line    gdp year if location == "South Sudan", xline(2011)
	twoway line    pop year if location == "Sudan" || line    pop year if location == "South Sudan", xline(2011)

	/*[I5] Soviet Union (USSR)
			- Ukraine, Uzbekistan, Kazakhstan, Belarus, Azerbaijan, Georgia, Tajikistan,
			  Moldova, Kyrgyz Republic, Lithuania, Turkmenistan, Armenia, Latvia, and Estonia
			  became independent (separated from Russia) in 1991.*/
	twoway line    srf year if location == "Russia"     || line    srf year if location == "Ukraine"         || ///
		   line    srf year if location == "Uzbekistan" || line    srf year if location == "Kazakhstan"      || ///
		   line    srf year if location == "Belarus"    || line    srf year if location == "Azerbaijan"      || ///
		   line    srf year if location == "Georgia"    || line    srf year if location == "Tajikistan"      || ///
		   line    srf year if location == "Moldova"    || line    srf year if location == "Kyrgyz Republic" || ///
		   line    srf year if location == "Lithuania"  || line    srf year if location == "Turkmenistan"    || ///
		   line    srf year if location == "Armenia"    || line    srf year if location == "Latvia"          || ///
		   line    srf year if location == "Estonia", xline(1991) legend(off)
	twoway line    oda year if location == "Russia"     || line    oda year if location == "Ukraine"         || ///
		   line    oda year if location == "Uzbekistan" || line    oda year if location == "Kazakhstan"      || ///
		   line    oda year if location == "Belarus"    || line    oda year if location == "Azerbaijan"      || ///
		   line    oda year if location == "Georgia"    || line    oda year if location == "Tajikistan"      || ///
		   line    oda year if location == "Moldova"    || line    oda year if location == "Kyrgyz Republic" || ///
		   line    oda year if location == "Lithuania"  || line    oda year if location == "Turkmenistan"    || ///
		   line    oda year if location == "Armenia"    || line    oda year if location == "Latvia"          || ///
		   line    oda year if location == "Estonia", xline(1991) legend(off)
	twoway line milper year if location == "Russia"     || line milper year if location == "Ukraine"         || ///
		   line milper year if location == "Uzbekistan" || line milper year if location == "Kazakhstan"      || ///
		   line milper year if location == "Belarus"    || line milper year if location == "Azerbaijan"      || ///
		   line milper year if location == "Georgia"    || line milper year if location == "Tajikistan"      || ///
		   line milper year if location == "Moldova"    || line milper year if location == "Kyrgyz Republic" || ///
		   line milper year if location == "Lithuania"  || line milper year if location == "Turkmenistan"    || ///
		   line milper year if location == "Armenia"    || line milper year if location == "Latvia"          || ///
		   line milper year if location == "Estonia", xline(1991) legend(off)
	twoway line milexp year if location == "Russia"     || line milexp year if location == "Ukraine"         || ///
		   line milexp year if location == "Uzbekistan" || line milexp year if location == "Kazakhstan"      || ///
		   line milexp year if location == "Belarus"    || line milexp year if location == "Azerbaijan"      || ///
		   line milexp year if location == "Georgia"    || line milexp year if location == "Tajikistan"      || ///
		   line milexp year if location == "Moldova"    || line milexp year if location == "Kyrgyz Republic" || ///
		   line milexp year if location == "Lithuania"  || line milexp year if location == "Turkmenistan"    || ///
		   line milexp year if location == "Armenia"    || line milexp year if location == "Latvia"          || ///
		   line milexp year if location == "Estonia", xline(1991) legend(off)
	twoway line    gdp year if location == "Russia"     || line    gdp year if location == "Ukraine"         || ///
		   line    gdp year if location == "Uzbekistan" || line    gdp year if location == "Kazakhstan"      || ///
		   line    gdp year if location == "Belarus"    || line    gdp year if location == "Azerbaijan"      || ///
		   line    gdp year if location == "Georgia"    || line    gdp year if location == "Tajikistan"      || ///
		   line    gdp year if location == "Moldova"    || line    gdp year if location == "Kyrgyz Republic" || ///
		   line    gdp year if location == "Lithuania"  || line    gdp year if location == "Turkmenistan"    || ///
		   line    gdp year if location == "Armenia"    || line    gdp year if location == "Latvia"          || ///
		   line    gdp year if location == "Estonia", xline(1991) legend(off)
	twoway line    pop year if location == "Russia"     || line    pop year if location == "Ukraine"         || ///
		   line    pop year if location == "Uzbekistan" || line    pop year if location == "Kazakhstan"      || ///
		   line    pop year if location == "Belarus"    || line    pop year if location == "Azerbaijan"      || ///
		   line    pop year if location == "Georgia"    || line    pop year if location == "Tajikistan"      || ///
		   line    pop year if location == "Moldova"    || line    pop year if location == "Kyrgyz Republic" || ///
		   line    pop year if location == "Lithuania"  || line    pop year if location == "Turkmenistan"    || ///
		   line    pop year if location == "Armenia"    || line    pop year if location == "Latvia"          || ///
		   line    pop year if location == "Estonia", xline(1991) legend(off)

	/*[I6] Yugoslavia
			- Macedonia became independent on Nov. 20, 1991.
			- Slovenia, Croatia, and Bosnia-Herzegovina became independent on Apr. 27, 1992.
			- Serbia, and Montenegro became independent in Jun. 2006. See above about Kosovo, becoming independent from Serbia.
			- NOTE: The former country, Yugoslavia (345), was already dissolved and no countries succeeded the polity
					unlike other cases as Indonesia to Russia, according to the Gleditsch & Ward country list.
					At the same time, the World Bank does not publish any data on the former Yugoslavia.
					However, my analysis covers the conflicts in Yugoslavia. To measure features of the government side
					in the Yugoslavian conflicts, I will calculate the values for Yugoslavia by aggregation of components.*/
	twoway line    srf year if location == "Slovenia"           || line    srf year if location == "Croatia"    || ///
		   line    srf year if location == "Bosnia-Herzegovina" || line    srf year if location == "Montenegro" || ///
		   line    srf year if location == "Macedonia (Former Yugoslav Republic of)", xline(1991 1992 2006)
	twoway line    oda year if location == "Slovenia"           || line    oda year if location == "Croatia"    || ///
		   line    oda year if location == "Bosnia-Herzegovina" || line    oda year if location == "Montenegro" || ///
		   line    oda year if location == "Macedonia (Former Yugoslav Republic of)", xline(1991 1992 2006)
	twoway line milper year if location == "Slovenia"           || line milper year if location == "Croatia"    || ///
		   line milper year if location == "Bosnia-Herzegovina" || line milper year if location == "Montenegro" || ///
		   line milper year if location == "Macedonia (Former Yugoslav Republic of)", xline(1991 1992 2006)
	twoway line milexp year if location == "Slovenia"           || line milexp year if location == "Croatia"    || ///
		   line milexp year if location == "Bosnia-Herzegovina" || line milexp year if location == "Montenegro" || ///
		   line milexp year if location == "Macedonia (Former Yugoslav Republic of)", xline(1991 1992 2006)
	twoway line    gdp year if location == "Slovenia"           || line    gdp year if location == "Croatia"    || ///
		   line    gdp year if location == "Bosnia-Herzegovina" || line    gdp year if location == "Montenegro" || ///
		   line    gdp year if location == "Macedonia (Former Yugoslav Republic of)", xline(1991 1992 2006)
	twoway line    pop year if location == "Slovenia"           || line    pop year if location == "Croatia"    || ///
		   line    pop year if location == "Bosnia-Herzegovina" || line    pop year if location == "Montenegro" || ///
		   line    pop year if location == "Macedonia (Former Yugoslav Republic of)", xline(1991 1992 2006)
*/

	/*Yugoslavia (345)*/
	*count
	*sum year
	set obs 11700
	replace location = "Yugoslavia" if location == ""
	replace GWccode = 345 if location == "Yugoslavia"
	*sum year if location == "Kosovo" & srf    != . /*1961-*/
	*sum year if location == "Kosovo" & oda    != . /*2009-*/
	*sum year if location == "Kosovo" & milper != . /*no data*/
	*sum year if location == "Kosovo" & milexp != . /*2008-*/
	*sum year if location == "Kosovo" & gdp    != . /*2000-*/
	*sum year if location == "Kosovo" & pop    != . /*1960-*/
	/*Yugoslavia has a number of missing values because of Kosovo.*/

	foreach var of varlist srf-pop {
		local i = 11641
		foreach num of numlist 1960/2019 {
			replace year = `num' in `i'
			quietly sum `var' if location == "Slovenia" & year == `num'
			scalar `var'_slo = r(mean)
			quietly sum `var' if location == "Bosnia-Herzegovina" & year == `num'
			scalar `var'_bih = r(mean)
			quietly sum `var' if location == "Croatia" & year == `num'
			scalar `var'_cro = r(mean)
			quietly sum `var' if location == "Macedonia (Former Yugoslav Republic of)" & year == `num'
			scalar `var'_mac = r(mean)
			quietly sum `var' if location == "Serbia" & year == `num'
			scalar `var'_ser = r(mean)
			quietly sum `var' if location == "Montenegro" & year == `num'
			scalar `var'_mon = r(mean)
			quietly sum `var' if location == "Kosovo" & year== `num'
			scalar `var'_kos = r(mean)
			replace `var' = `var'_slo + `var'_bih + `var'_cro + `var'_mac + `var'_ser + `var'_mon + `var'_kos if location == "Yugoslavia" & year == `num' & year <= 1991
			replace `var' = `var'_slo + `var'_bih + `var'_cro + `var'_ser + `var'_mon + `var'_kos if location == "Yugoslavia" & year == `num' & year == 1992
			replace `var' = `var'_ser + `var'_mon + `var'_kos if location == "Yugoslavia" & year == `num' & year >= 1993 & year <= 2006
			replace `var' = `var'_ser + `var'_kos if location == "Yugoslavia" & year == `num' & year >= 2007 & year <= 2008
			replace `var' = `var'_ser if location == "Yugoslavia" & year == `num' & year >= 2009
			local i = `i' + 1
		}
	}

/*(d) Others*/
	sort GWccode year
	order location GWccode, first
	label variable location "Country name"
	label variable GWccode  "Country code"
	label variable year     "Year"
	label variable srf      "Surface area (sq. km)"
	label variable oda      "Net Official Development Assistance (current USD)"
	label variable milper   "Military personnel"
	label variable milexp   "Military expenditure (current USD)"
	label variable gdp      "Gross Domestic Product (current USD)"
	label variable pop      "Population"
	save "Data/03. Controls/Ctrl2_World_Bank_Open_Data.dta", replace
	*sum     milper milexp if year >= 1989 /*After 1989, milper has less missing values than milexp.*/
	*scatter milper milexp if year >= 1989



*------------------------------------------------------------------------------*
/*(3) Varieties of Democracy (V-Dem)                                          */
*------------------------------------------------------------------------------*
	use "RawData/03. Controls/V-Dem/V-Dem-CY-Core-v9.dta", clear
	keep country_name year v2x_polyarchy
	rename country_name location

/*(a) Merge with G&W country codes:
	  V-Dem only has V-Dem Country ID, not matched with G&W codes.*/
	*tab location
	replace location = "Bosnia-Herzegovina"            if location == "Bosnia and Herzegovina"
	replace location = "Myanmar"                       if location == "Burma/Myanmar"
	replace location = "Congo, Democratic Republic of" if location == "Democratic Republic of the Congo"
	replace location = "German Federal Republic"       if location == "Germany"
	replace location = "Hesse-Darmstadt (Ducal)"       if location == "Hesse-Darmstadt"
	replace location = "Hesse-Kassel (Electoral)"      if location == "Hesse-Kassel"
	replace location = "Côte d'Ivoire"                 if location == "Ivory Coast"
	replace location = "Kyrgyz Republic"               if location == "Kyrgyzstan"
	replace location = "Macedonia (Former Yugoslav Republic of)" if location == "Macedonia"
	replace location = "Mecklenburg-Schwerin"          if location == "Mecklenburg Schwerin"
	replace location = "Korea, People's Republic of"   if location == "North Korea"
	replace location = "Vietnam, Republic of"          if location == "Republic of Vietnam"
	replace location = "Congo"                         if location == "Republic of the Congo"
	replace location = "Rumania"                       if location == "Romania"
	replace location = "São Tomé and Principe"         if location == "Sao Tome and Principe"
	replace location = "Korea, Republic of"            if location == "South Korea"
	replace location = "Yemen, People's Republic of"   if location == "South Yemen"
	replace location = "Surinam"                       if location == "Suriname"
	replace location = "Gambia"                        if location == "The Gambia"
	replace location = "East Timor"                    if location == "Timor-Leste"
	replace location = "Württemberg"                   if location == "Würtemberg"

	merge m:1 location using "Data/03. Controls/Ctrl1_GWccodes.dta"
	*tab location if _merge == 1
	*tab location if _merge == 2
	drop if _merge == 1 | _merge == 2
	drop _merge


/*(b) Data checks: annexation and independence/dissolution
	  Timor-Leste and Namibia are not included in the former ruling countries' data,
	  but others are included according to the V-Dem Country Coding Units v.11.1.
	  Because only Yugoslavia -> Serbia changed country codes among the government side
	  according to the G&W country list, I will make obs for Yugoslavia like the World Bank data.
	  Note that there are some mismatches with the World Bank data's specification ...

	  [I1] Timor-Leste (Indonesia):
			- "Coded: 1900- ...
			   Democratic Republic of Timor-Leste under Indonesian occupation (1975–1999);
			   Democratic Republic of Timor-Leste under UN administration (1999-2002);
			   Democratic Republic of Timor-Leste [independent state] (2002– )."
			  (V-Dem Country Coding Units v11.1, p.24)
	  [I3] South Africa:
			- "Includes only territories formally within the boundaries of the country,
			   not territories that South Africa exercised influence or colonial dominion over."
			  (V-Dem Country Coding Units v11.1, p.13)
	  [I4] Sudan:
			- "Excludes South Sudan from the date of its independence (2011)."
			  (V-Dem Country Coding Units v.11.1, p.12)
	  [I5] Russia:
			- "Russian Socialist Federative Republic (USSR) (1918–1991) ...
			   Contemporary Russia is a much smaller region than the USSR, since after 1991
			   it no longer included Belarus, Armenia, Azerbaijan, Georgia, Moldova, the Ukraine,
			   the Baltic republics, and the central Asian republics."
			  (V-Dem County Coding Units v11.1, p.29)
	  [I2] Serbia / [I6] Yugoslavia:
			- "Includes Slovenia up to 1989, Croatia up to 1991, Macedonia up to 1991,
			   Bosnia and Herzegovina up to 1992, and Kosovo up to 1999. Between 1998 and 2006
			   refers only to the Republic of Serbiaconstituent country of Yugoslavia and
			   then the State Union. Montenegro is coded separately for this period."
			  (V-Dem County Coding Units v11.1, p.27)
			- Conflict ID 412 (for Kosovo independence) continued even after Kosovo was excluded
			  from Serbia in the V-Dem data. Against which government(s) did Kosovo fight?
			  Timor-Leste and Namibia are not included in the ruling countries's data,
			  but the government side of their conflicts may be the only the ruling countries
			  excluding the territories. How about conflicts against Yugoslavia after the dissolution?
			  It is unclear, and thus, based on the G&W country list, I will make the data for
			  Yugoslavia by the average values of component countries.*/

	/*Yugoslavia (345)*/
	count
	sum year
	set obs 26319
	replace location = "Yugoslavia" if location == ""
	replace GWccode = 345 if location == "Yugoslavia"
	*sum year if v2x_polyarchy != . & location == "Slovenia"           /*1989-*/
	*sum year if v2x_polyarchy != . & location == "Bosnia-Herzegovina" /*1992-*/
	*sum year if v2x_polyarchy != . & location == "Croatia"            /*1941-44; 1991-*/
	*sum year if v2x_polyarchy != . & location == "Macedonia (Former Yugoslav Republic of)" /*1991-*/
	*sum year if v2x_polyarchy != . & location == "Serbia"             /*1834-*/
	*sum year if v2x_polyarchy != . & location == "Montenegro"         /*1900-18; 1998-*/
	*sum year if v2x_polyarchy != . & location == "Kosovo"             /*1999-*/

	local i = 26089
	foreach num of numlist 1789/2019 {
		replace year = `num' in `i'
		quietly sum v2x_polyarchy if location == "Slovenia" & year == `num'
		scalar v2x_slo = r(mean)
		quietly sum v2x_polyarchy if location == "Bosnia-Herzegovina" & year == `num'
		scalar v2x_bih = r(mean)
		quietly sum v2x_polyarchy if location == "Croatia" & year == `num'
		scalar v2x_cro = r(mean)
		quietly sum v2x_polyarchy if location == "Macedonia (Former Yugoslav Republic of)" & year == `num'
		scalar v2x_mac = r(mean)
		quietly sum v2x_polyarchy if location == "Serbia" & year == `num'
		scalar v2x_ser = r(mean)
		quietly sum v2x_polyarchy if location == "Montenegro" & year == `num'
		scalar v2x_mon = r(mean)
		quietly sum v2x_polyarchy if location == "Kosovo" & year== `num'
		scalar v2x_kos = r(mean)
		replace v2x_polyarchy = v2x_ser if location == "Yugoslavia" & year == `num' & year <= 1988
		replace v2x_polyarchy = (v2x_ser + v2x_slo)/2 if location == "Yugoslavia" & year == `num' & year >= 1989 & year <= 1990
		replace v2x_polyarchy = (v2x_ser + v2x_slo + v2x_cro + v2x_mac)/4 if location == "Yugoslavia" & year == `num' & year == 1991
		replace v2x_polyarchy = (v2x_ser + v2x_slo + v2x_cro + v2x_mac + v2x_bih)/5 if location == "Yugoslavia" & year == `num' & year >= 1992 & year <= 1997
		replace v2x_polyarchy = (v2x_ser + v2x_slo + v2x_cro + v2x_mac + v2x_bih + v2x_mon)/6 if location == "Yugoslavia" & year == `num' & year == 1998
		replace v2x_polyarchy = (v2x_ser + v2x_slo + v2x_cro + v2x_mac + v2x_bih + v2x_mon + v2x_kos)/7 if location == "Yugoslavia" & year == `num' & year >= 1999
		local i = `i' + 1
	}
	drop if year <= 1988 /*I can keep all years, but the values before 1988 may be wrong (and not used for analysis).*/


/*(c) Others*/
	sort location year
	order location GWccode, first
	label variable location      "Country name"
	label variable GWccode       "Country code"
	label variable year          "Year"
	label variable v2x_polyarchy "Electoral democracy index"
	save "Data/03. Controls/Ctrl3_V-Dem.dta", replace



*------------------------------------------------------------------------------*
/*(4) MILINDA                                                                 */
*------------------------------------------------------------------------------*
/*Note that MILINDA has quite many miscodings. I asked some questions about the codings
  to Jetschke and Schlipphak, but unfortunately, they did not answer my questions or correct
  the miscodings properly. However, MILINDA comprehensively covers the cases coded in other datasets on non-UN peacekeeping.*/

/*(a) Keep non-UN missions only*/
	import excel using "RawData/03. Controls/Non-UN PKOs/MILINDA-v1-March2019.xls", firstrow clear

	*tab acronym if agent == "UN", m
	/*Some UN missions are not included in my UNPO lists:
	  [1] Collective security (under Chapter VII)
		   - 1st gulf war = UNSCR 678 (use of force against Iraq)
		   - Korean War   = UNSCR 83 (UN forces)
	  [2] For extrasystemic wars
		   - UNGOC        = UN Good Offices Committee Indonesia
		   - UNCI         = UN Commission for Indonesia
	  [3] For interstate wars
		   - UNCIP        = UN Commission for India and Pakistan
		   - UNTCOK       = UN Temporary Commission on Korea
		   - UNCURK       = UN Commission for the Unification and Rehabilitation of Korea
	  [4] Related to other UN missions
		   - MNF          = Multinational Force to Government of Haiti (with MICIVIH)
							(<https://search.archives.un.org/downloads/united-nations-political-and-peacekeeping-missions-in-haiti-1993-2001.pdf> last access on Mar. 4, 2020)
		   - UNCIAV       = UN Joint Verification and Support Commission (with ONUCA)
		   - UNMLT        = UN Military Liaison Team Cambodia (following UNAMIC/UNTAC)
							(<https://www.veterans.gc.ca/eng/remembrance/medals-decorations/details/151> last access on Mar. 4, 2020)
	  [5] Others
		   - UNSCOB       = UN Special Committee on the Balkans
		   - UNPOC        = ?
		   - .            = police officers component in Burundi

	  Note that UNIFIL is separated into I and II, but they are the same in my dataset.
	  I and II are separated because of UNSCR 1701, which enhanced UNFIL's mandate.*/
	drop if agent == "UN"

	*tab acronym if agent != "UN"
	/*Some UN missions are coded as non-UN unlike my dataset:
	   - MINURCAT: agent == "EU"
	     This coding might be incorrect. EU deployed a mission EUFOR TChad/RCA,
	     and the missions seem to be listed on the MILINDA (i.e., EUFOR Chad and
		 EUFOR Central African Republic I and II).
	   - UNAMID  : agent == "UNHYBRID"*/
	drop if acronym == "MINURCAT" | acronym == "UNAMID"


/*(b) Start and end years*/
	/*Starting time:
	  yearbeg_n is set as mdy format in Stata, but according to the MILINDA codebook,
	  yearbeg_n is a "numerical variable, stating the start year of a mission."
	  Because of this, I cannot see the monthly variation of non-UN PKO presence.*/

	*count
	*count if real(yearbeg) == year(yearbeg_n) /*(189-184=) 5 missions not matched*/
	*list acronym yearbeg yearbeg_n yearend srcoth if real(yearbeg) != year(yearbeg_n)
	/*[1] EUPOL (Macedonia):
		  "The Mission, code-named EUPOL PROXIMA, was launched on 15 December 2003"
		  (<https://eeas.europa.eu/archives/csdp/missions-and-operations/proxima-fyrom/index_en.htm> last access on Oct. 30, 2022)
	  [2] EUROPOL Afghanistan:
		  "EUPOL Afghanistan (European Union Police Mission in Afghanistan) is a civilian Common Security and Defence Policy (CSDP) mission.
		   It has since its establishment in 2007 supported the reform efforts of the Afghan Government in ..."
		  (<https://eeas.europa.eu/archives/csdp/missions-and-operations/eupol-afg/index.html> last access on Oct. 30, 2022)
	  [3] IMT II:
		  "Malaysia then decided to withdraw its contingent in November 2008 and the IMT temporarily suspended
		   its activities in 2009. The IMT became active again in February 2010, ..."
		  (<https://en.wikipedia.org/wiki/International_Monitoring_Team> last access on Oct. 30, 2022)
		  IMT I was operated 2004-Nov. 2008 according to the MILINDA. See Mullenbach's dataset v.3.1 (OBSNUM == 40) as well.
	  [4] EUFOR Central African Republic II:
		  "On 20 January 2014, the EU Foreign Affairs Ministers Council ap­proved a Task Force led by the European Union
		   in the Central African Republic. The European Union Force in the Central African Republic (EU­FOR - RCA) was
		   established on the legal basis of United Nations Secu­rity Council Resolution 2 134 (2014)."
		  (<https://www.eeas.europa.eu/eutm-rca/eutm-rca-european-training-mission-central-african-republic-military-mission_en?s=334> last access on Oct. 30, 2022)
		  Note that EUFOR Central African Republic I is EUFOR Tchad/RCA since 2008.
	  [5] Long Term Mission to Estonia:
		  "The OSCE mission, which consisted of six personnel, began its work in Tallinn and Narva on February 15, 1993."
		  (<https://uca.edu/politicalscience/dadm-project/europerussiacentral-asia-region/estonia-1991-present/> last access on Oct. 30, 2022)*/
	
	gen start_yr = real(yearbeg)
	recode start_yr (2004=2010) if acronym == "IMT II"

	/*Ending time*/
	split yearend, p(" ") gen(yearend_)

	*tab yearend_1, m
	/*[1] The MILINDA covers 1947-2016 (MILINDA codebook, p.1).
		  However, the only ECOMIB, whose withdrawal ECOWAS decided in Sep. 2020,
		  ended in 2017 according to MILINDA. Note that the analysis ignores
		  the (potential) existence of non-UN PKOs established in 2017-2018.

	  [2] EUPT (Serbia Kosovo) has a missing value on end date,
		  but the missions should have ended by Dec. 2008 before the initial
		  operational capability was built for the succeeding mission EULEX.
		  "EULEX was preceded by the EU Planning Team (EUPT Kosovo) which was established by the Council
		   in April 2006 in order to prepare for a possible rule of law operation. EULEX was created
		   by Council Joint Action 2008/124/CFSP of 4 February 2008 and deployed during 2008 to reach
		   its initial operational capability in early December 2008 and its full operational capability
		   on 6 April 2009. Its mandate has been extended until 14 June 2012."
		  (<https://www.europarl.europa.eu/meetdocs/2009_2014/documents/cont/dv/5_briefing_on_eulex_/5_briefing_on_eulex_en.pdf> last access on Mar. 4, 2020)

		  Note that EULEX started in 2008 according to the MILINDA:
		   - br if acronym == "EUPT" | acronym == "EULEX Kosovo"*/
	replace yearend_2 = "12"      if yearend_1 == "."
	replace yearend_1 = "2008"    if yearend_1 == "."
	replace yearend_1 = "ongoing" if yearend_1 == "2017"
	replace yearend_1 = "2006"    if yearend_1 == "2006."

	*tab yearend_2, m
	replace yearend_2 = "1"  if yearend_2 == "January"
	replace yearend_2 = "2"  if yearend_2 == "February"
	replace yearend_2 = "3"  if yearend_2 == "March"
	replace yearend_2 = "4"  if yearend_2 == "April"
	replace yearend_2 = "5"  if yearend_2 == "May"
	replace yearend_2 = "6"  if yearend_2 == "June"
	replace yearend_2 = "7"  if yearend_2 == "July"
	replace yearend_2 = "8"  if yearend_2 == "August"
	replace yearend_2 = "9"  if yearend_2 == "September" | yearend_2 == "Septmber"
	replace yearend_2 = "10" if yearend_2 == "October"
	replace yearend_2 = "11" if yearend_2 == "November" | yearend_2 == "Nov"
	replace yearend_2 = "12" if yearend_2 == "December" | yearend_2 == "Deceember" | yearend_2 == "Dec"

	gen end_yr = real(yearend_1) if yearend_1 != "ongoing" | yearend_1 != "."
	replace end_yr = 2016 if yearend_1 == "ongoing"
	gen end_mon = real(yearend_2)
	replace end_mon = 12 if yearend_1 == "ongoing"

	*gen x = ym(end_yr, end_mon) - ym(start_yr,1) + 1
	*scatter x duram_n if yearend_1 != "ongoing" /*A variable on the mission duration also has incorrect values?*/
	*drop x

	/*Ongoing cases:
	  I checked if the missions coded as ongoing (until Dec. 2016) were still ongoing
	  until Dec. 2018, the last month of my analysis. The only one mission already ended before 2018.
	   - MISCA: "On 15 September 2014, the African-led International Support Mission in the Central African Republic (MISCA)
	             transferred its authority over to MINUSCA, in accordance with resolution 2149 (2014)"
				(<https://peacekeeping.un.org/en/mission/minusca> last access on Oct. 30, 2022)*/
	*br if yearend_1 == "ongoing"
	replace end_yr = 2014 if acronym == "MISCA"
	replace end_mon = 9   if acronym == "MISCA"
	replace end_yr = 2018 if yearend_1 == "ongoing"
	replace end_mon = 12  if yearend_1 == "ongoing"


/*(c) Mission-year data*/
	expand end_yr - start_yr + 1
	bysort caseid: gen x = _n
	gen year = start_yr if x == 1
	by caseid: replace year = year[_n-1] + 1 if year == .

	*tab agent intcat, m
	*list if agent == "NONSTA" /*AMM is coded as a missing value for 'intcat,' but it is coded as individual states' PKO in Mullenbach's dataset v.3.1 (TPTYPE == 3).*/
	gen ropko = 0
	replace ropko = 1 if intcat == "RORO" | intcat == "UNRO"
	gen stpko = 0
	replace stpko = 1 if intcat == "ISIS" | intcat == "ROIS" | intcat == "UNIS" | intcat == "."
	*tab ropko stpko, m


/*(d) Conflict-month data*/
	/*UCDP conflict ID:
	  To code host conflicts' ID for each non-UN PKO, I checked conflict incompatibilities and start/end dates
	  that are coded in the UCDP ACD v.19.1, and non-UN PKO start/end years (dates) that are coded in the MILINDA
	  (and Mullenbach's dataset as well).*/
	gen expand = 1
	recode expand (1=2) if caseid == 225 /*Gerogia - Abkhazia / South Ossetia*/
	recode expand (1=3) if caseid == 109 | caseid == 110 | caseid == 122 | caseid == 125 | caseid == 218 | caseid == 280 /*Bosnia-Herzegovina*/ | caseid == 283 /*Georgia*/
	expand expand
	bysort caseid year: gen dup = _n

	gen conflict_id = .
	recode conflict_id (.=201) if caseid == 172                                 /*ICSC-Cambodia*/
	recode conflict_id (.=208) if caseid == 171                                 /*ICSC-Laos*/
	recode conflict_id (.=230) if caseid == 167                                 /*Arab League Military Obserbers to North Yemen*/
	recode conflict_id (.=234) if caseid == 186 | caseid == 285                 /*TIPH and EUPOL-COPPS*/
	recode conflict_id (.=235) if caseid == 170                                 /*Neutral Nations Supervisory Commission*/
	recode conflict_id (.=248) if caseid == 175                                 /*Joint Truce Force Cyprus*/
	recode conflict_id (.=249) if caseid == 173                                 /*ISCS Vietnam*/
	recode conflict_id (.=259) if caseid == 116 | caseid == 126 | caseid == 199 /*NTM-I, EUJUST LEX, and MNF-Iraq*/
	recode conflict_id (.=260) if caseid == 168 | caseid == 169 | caseid == 196 | caseid == 257 | caseid == 282 /*ADF, MNF I, II, and SASF | Syrian inntervention in Lebanon(?)*/
	recode conflict_id (.=262) if caseid == 174                                 /*ICSC-Laos II*/
	recode conflict_id (.=265) if caseid == 209 | caseid == 228                 /*Nigerian PKM in Congo-Kinshasa and IAF Congo-Kinshasa*/
	recode conflict_id (.=271) if caseid == 229 | caseid == 237                 /*MNPF-Iraq and Operation Provide Comfort*/
	recode conflict_id (.=278) if caseid ==  92                                 /*Bamako Ceasefire Commission*/
	recode conflict_id (.=283) if caseid == 101 | caseid == 123 | caseid == 130 | caseid == 131 | caseid == 134 | caseid == 136 /*OAU Joint Monitoring Commission, EUIEMF, EUPOL, EUFOR, EUROPOL, and EUSEC*/
	recode conflict_id (.=287) if caseid ==  99 | caseid == 161 | caseid == 192 | caseid == 224 /*OMIB, AMIB, SAPSD, and AUSTF*/
	recode conflict_id (.=288) if caseid ==  93 | caseid ==  94 | caseid ==  95 | caseid == 139 | caseid == 279 /*Chad I, II, III, EUFOR, and French PKM*/
	recode conflict_id (.=289) if caseid == 107                                 /*MAPP/OEA*/
	recode conflict_id (.=290) if caseid == 104                                 /*OAS IAPF*/
	recode conflict_id (.=293) if caseid == 178                                 /*ICCS*/
	recode conflict_id (.=299) if caseid == 262                                 /*OMS-Syria*/
	recode conflict_id (.=306) if caseid == 105 | caseid == 106                 /*Obsrvs I and II*/
	recode conflict_id (.=308) if caseid == 230 | caseid == 231 | caseid == 232 /*IMT I, II, and OICMT*/
	recode conflict_id (.=309) if caseid == 127 | caseid == 163 | caseid == 193 | caseid == 206 | caseid == 214 | caseid == 215 | caseid == 256 /*AMIS I, II, and EU Support to AMIS | CPMT, VMT, and IMU*/
	recode conflict_id (.=318) if caseid == 149 | caseid == 261                 /*CMF-Zimbabwe and Commonwealth Monitoring Force*/
	recode conflict_id (.=330) if caseid == 189                                 /*INTERFET*/
	recode conflict_id (.=332) if caseid == 250                                 /*JVM*/
	recode conflict_id (.=333) if caseid == 115 | caseid == 137 | caseid == 191 /*ISAF and EUROPOL*/
	recode conflict_id (.=336) if caseid == 260                                 /*OASCIAV*/
	recode conflict_id (.=337) if caseid == 117 | caseid == 141 | caseid == 165 | caseid == 180 | caseid == 208 /*Operation Ocean Shield, IGASOM, AMISOM, EUTM, and UNITAF*/
	recode conflict_id (.=341) if caseid == 142 | caseid == 145 | caseid == 213 /*ECOMOG, ECOMIL, and Joint Task Force*/
	recode conflict_id (.=350) if caseid == 248                                 /*Caribbean Peacekeeping Force in Grenada*/
	recode conflict_id (.=352) if caseid == 194 | caseid == 207                 /*SLMM and IPKF*/
	recode conflict_id (.=355) if caseid == 179                                 /*Benin Observer Commission*/
	recode conflict_id (.=362) if caseid == 162 | caseid == 164 | caseid == 166 | caseid == 233 /*OMIC III, IV, MAES I-II, and AMISEC*/
	recode conflict_id (.=366) if caseid == 132 | caseid == 227 | caseid == 265 /*AMMs and EU IMP*/
	recode conflict_id (.=369) if caseid == 184 | caseid == 187 | caseid == 276 | caseid == 277 /*TMG, PMG, Australia-NZ police assistance mission, and SPRPF*/
	recode conflict_id (.=374) if caseid ==  96 | caseid ==  97 | caseid ==  98 | caseid == 181 | caseid == 251 /*OAU MOT, NMOG I, II, Operation Turqouise, and Support Hope*/
	recode conflict_id (.=379) if caseid == 274                                 /*French PKM*/
	recode conflict_id (.=380) if caseid == 283 & dup == 1                      /*OSCE Mission to Georgia*/
	recode conflict_id (.=381) if caseid == 108 | caseid == 200 | caseid == 270 /*OAS Special Mission, MIF, and MNF (Operation Uphold Democracy)*/
	recode conflict_id (.=382) if caseid == 143 | caseid == 150                 /*ECOMOG and CPDTF*/
	recode conflict_id (.=384) if caseid == 217                                 /*ECMM*/
	recode conflict_id (.=390) if caseid == 216                                 /*ECMM; Conflict ID 385 transformed to 390.*/
	recode conflict_id (.=389) if (caseid == 109 | caseid == 110 | caseid == 122 | caseid == 125 | caseid == 218 | caseid == 280 | caseid == 290) & dup == 1 /*IFOR, SFOR, EUFOR-Althea, EUPM, ECMM, WEU-PF, and OSCE Mission to BiH*/
	recode conflict_id (.=390) if caseid == 234 | caseid == 292                 /*OSCE Police Monitoring Group and OSCE Mission to Croatia*/
	recode conflict_id (.=392) if caseid == 156 | caseid == 157 | (caseid == 225 & dup == 1) | (caseid == 283 & dup == 2) /*Russian PKF, CPKF, OSCE Mission to Georgia, and EUMM*/
	recode conflict_id (.=393) if caseid == 154 | (caseid == 225 & dup == 2) | caseid == 235 | (caseid == 283 & dup == 3) /*JPKF, OSCE Monitoring Mission, OSCE Mission to Georgia, and EUMM*/
	recode conflict_id (.=394) if caseid == 128 | caseid == 153 | caseid == 222 | caseid == 294 /*EUBAM, Military observation mission, JCC, OSCE Mission to Moldova*/
	recode conflict_id (.=395) if caseid == 155                                 /*CIS Buffer Force*/
	recode conflict_id (.=397) if (caseid == 109 | caseid == 110 | caseid == 122 | caseid == 125 | caseid == 218 | caseid == 280 | caseid == 290) & dup == 2 /*IFOR, SFOR, EUFOR-Althea, EUPM, ECMM, WEU-PF, and OSCE Mission to BiH*/
	recode conflict_id (.=398) if (caseid == 109 | caseid == 110 | caseid == 122 | caseid == 125 | caseid == 218 | caseid == 280 | caseid == 290) & dup == 3 /*IFOR, SFOR, EUFOR-Althea, EUPM, ECMM, WEU-PF, and OSCE Mission to BiH*/
	recode conflict_id (.=403) if caseid == 182                                 /*MOMEP*/
	recode conflict_id (.=407) if caseid ==  91 | caseid == 100                 /*OMIC I and II*/
	recode conflict_id (.=410) if caseid == 140 | caseid == 144 | caseid == 147 /*EUSSR, ECOMOG, and ECOMIB*/
	recode conflict_id (.=411) if caseid == 188                                 /*SADC Lesotho*/
	recode conflict_id (.=412) if caseid ==  20 | caseid == 111 | caseid == 135 | caseid == 138 | caseid == 202 | caseid == 203 | caseid == 220 | caseid == 221 | caseid == 278 | caseid == 284 /*OSCE Mission to Serbia, KFOR, EULEX, EUPT, Operation Allied Force, Eagle Eye, NATO AFOR, OSCE KVM, BMM, and OMiK*/
	recode conflict_id (.=416) if caseid == 158 | caseid == 159 | caseid == 185 | caseid == 204 | caseid == 223 | caseid == 241 | caseid == 242 | caseid == 246 | caseid == 267 | caseid == 275 /*FOMUC, MICOPAX, MISAB, MISCA, EUFOR I, II, Operation Sangaris, Boali, AFISM-CAR, and CEN-SAD*/
	recode conflict_id (.=417) if caseid == 112 | caseid == 113 | caseid == 114 | caseid == 121 | caseid == 124 | caseid == 129 | caseid == 258 | caseid == 264 | caseid == 266 | caseid == 289 /*Operation Essential Harvest, Amber Fox, Allied Harmony, EUPOL, EUFOR, EUPAT, OSCE Spillover Monitor Mission, OSCE Mission to Skopje, and ECMM/EUMM*/
	recode conflict_id (.=419) if caseid == 146 | caseid == 197                 /*ECOMICI, Operation Licorne*/
	recode conflict_id (.=11343) if caseid == 195                               /*MFO*/
	recode conflict_id (.=11346) if caseid == 118                               /*Operation Unified Protector*/
	recode conflict_id (.=11347) if caseid == 148 | caseid == 253               /*AFISMA, Operation Serval*/
	recode conflict_id (.=13219) if caseid == 236                               /*OSCE SMM*/
	recode conflict_id (.=13306) if caseid == 226                               /*OSCE Observer Mission at the Russian Checkpoints Gukovo and Donetsk; Conflict ID 13246 (and 13247) transformed to 13306.*/

	*tab caseid if conflict_id == .
	/*27 missions do not have matched conflicts in the UCDP ACD.
	  [1]  caseid == 103            : 1955 Caledonista invasion of Costa Rica is not listed in the ACD. (I am not sure that OAS Committee of Military Experts was deployed to this invasion though.)
	  [2]  caseid == 119            : 2011 Bahraini uprising is not listed in the ACD.
	  [3]  caseid == 120            : ECMM/EUMM for Western Balkans -> This mission would be a regional mission.
	  [4]  caseid == 133            : EUSR BST (Border Support Team) -> This mission was deployed for security of the border with Russia.
	  [5]  caseid == 151 / 152 / 190 / 198: Ethnic violence in Solomon Islands (1999-2003)
	  [6]  caseid == 176            : Black September in Jordan is not listed in the ACD.
	  [7]  caseid == 177            : Interstate conflict between Tanzania and Uganda happened in 1978 on ACD, but Somali Military Observer Team was active only in 1972.
	  [8]  caseid == 183 / 205 / 219 / 243 / 244 / 245: 1991 fall of communist regime / 1997 Albanian Civil Unrest is not listed in the ACD.
	  [9]  caseid == 201            : 2006 East Timor crisis is not listed in the ACD.
	  [10] caseid == 210            : Interstate conflict between Chad and Libya happened in 1987 on ACD, but Inter-African Force was active only in 1981-82.
	  [11] caseid == 254            : Papua New Guinea did not have conflicts over governments.
	  [12] caseid == 255            : 1964 Zanzibar rebellion is not listed in the ACD.
	  [13] caseid == 263            : 2006 Nuku'alofa riots is not listed in the ACD.
	  [14] caseid == 271 / 287      : Togo / Ukraine did not have conflicts in the time period.
	  [15] caseid == 286 / 288 / 293: Estonia / Montenegro / Latvia never have conflicts on ACD.
	  [16] caseid == 291            : 2003 Rose Revolution in Georgia is not listed in the ACD.*/
	drop x expand dup
	drop if conflict_id == .

	/*Delete duplicates*/
	sort conflict_id year caseid
	foreach var of varlist ropko stpko {
		by conflict_id year: egen max_`var' = max(`var')
		replace `var' = max_`var'
		drop max_`var'
		by conflict_id year: egen num_`var' = sum(`var')
	}
	duplicates drop conflict_id year, force

	keep conflict_id year ropko stpko num_ropko num_stpko
	order conflict_id year ropko stpko num_ropko num_stpko, first
	label variable conflict_id "Conflict ID"
	label variable year        "Year"
	label variable ropko       "PKO presence by regional organizations"
	label variable stpko       "PKO presence by individual states"
	label variable num_ropko   "# of PKOs by regional organizations"
	label variable num_stpko   "# of PKOs by individual states"
	save "Data/03. Controls/Ctrl4_MILINDA.dta", replace





********************************************************************************
********************************************************************************
********************************************************************************
/*Merge*/
	use "Data/01. Conflicts/UCDP_datasets.dta", clear

/*(a) UN PKOs*/
	merge m:1 conflict_id ym year month using "Data/02. Peacekeeping/UNPO_datasets.dta"
	*tab _merge                                /*2: 4,011 obs / 3: 3,595 obs*/
	*tab _merge if year >= 1989 & year <= 2018 /*2: 2,037 obs / 3: 3,595 obs*/
	*tab pko1 if _merge == 2 & year >= 1989 & year <= 2018
	/*_merge == 2 cases:
	  [1] Extrasystemic/Interstate wars
		   - UNASOG, UNDOF, UNFICYP, UNGOMAP, UNIIMOG, UNIKOM, UNMEE, UNMOGIP, and UNTSO.
	  [2] Independence
		   - UNISFA  : After South Sudan became independent from Sudan, conflict ID 11344 is regarded as an interstate conflict.
		   - UNMIK   : After Kosovo became independent from Serbia, conflict ID 412 cannot be occurred anymore.
		   - UNMISET : After East Timor became independent from Indonesia, conflict ID 330 cannot be occurred anymore.
	  [3] Expenditures before conflicts started
		   - UNMISS  : Conflict ID 11345 started in August 2011, but UNMISS started in July 2011.
		   - UNPROFOR: Conflict ID 389, 397, and 398 started after UNPROFOR was established.*/

	foreach var of varlist pko pko_robust militarybdg-ln_pop_officernat {
		recode `var' (.=0) if _merge == 1
	}
	drop if _merge == 2
	drop _merge


/*(b) UN SPMs*/
	merge m:1 conflict_id ym year month using "Data/02. Peacekeeping/UNPO3_spm_conflict-month.dta"
	*tab _merge                                /*2: 130 obs / 3: 2,433 obs*/
	*tab _merge if year >= 1989 & year <= 2018 /*2:  31 obs / 3: 2,433 obs*/
	*tab spm1 if _merge == 2 & year >= 1989 & year <= 2018
	/*_merge == 2 cases:
	   - BONUCA: Conflict ID 416 started after BONUCA was established.
	   - UNOTIL: After East Timor became independent from Indonesia, conflict ID 330 cannot be occurred anymore.*/

	recode spm        (.=0) if _merge == 1
	recode spm_robust (.=0) if _merge == 1
	drop if _merge == 2
	drop _merge


/*(c) Other controls*/
	/*World Bank data*/
	merge m:1 GWccode year using "Data/03. Controls/Ctrl2_World_Bank_Open_Data.dta"
	*tab location year if _merge == 1 /*South Yemen in 1989 and 1990*/
	drop if _merge == 2
	drop _merge

	foreach var of varlist srf-pop {
		gen ln_`var' = ln(`var')
	}

	/*V-Dem data*/
	merge m:1 GWccode year using "Data/03. Controls/Ctrl3_V-Dem.dta"
	/*_merge == 1: 0 obs; Note that Yugoslavia data exist even after the Yugoslavia dissolved because the peace spell for the conflict against the government of Yugoslavia remains in the data.*/
	drop if _merge == 2
	drop _merge

	/*MILINDA*/
	merge m:1 conflict_id year using "Data/03. Controls/Ctrl4_MILINDA.dta"
	*tab conflict_id if _merge == 2 & year >= 1989
	/*[1] Interstate wars         : 235, 403, and 11343
	  [2] Before conflicts started: 397, 398, 416, and 417
	  [3] After independence      : 412*/
	foreach var of varlist ropko-num_stpko {
		recode `var' (.=0) if _merge == 1
	}
	drop if _merge == 2
	drop _merge


/*(d) Other variables for analysis*/
	gen time = ym - ym(year(start_date), month(start_date)) + 1

	sort conflict_id ym
	xtset conflict_id ym
	label data "Replication data for Does Peacekeeping by Civilians Work?"
	save "Data/Dataset_monthly.dta", replace





********************************************************************************
*                                 Annual data                                  *
********************************************************************************

/*UCDP datasets*/
	use "Data/01. Conflicts/UCDP_datasets.dta", clear
	sort conflict_id year month
	drop ym month num_dyad_ged /*# of dyads (annual) can be created if needed*/


/*(a) Replace values on the annual basis*/
	foreach var of varlist bd_best_ged-bd_unk_ged num_event_ged num_nsv-bd_total_osv_rebel {
		by conflict_id year: egen missing = max(missing(`var'))
		by conflict_id year: egen sum_`var' = sum(`var') if missing == 0
		replace `var' = sum_`var'
		drop missing sum_`var'
	}
	foreach var of varlist pa-pa_signed *_binary {
		by conflict_id year: egen missing = max(missing(`var'))
		by conflict_id year: egen max_`var' = max(`var') if missing == 0
		replace `var' = max_`var'
		drop missing max_`var'
	}


/*(b) "Actor ceases to exist" cases:
	  The government side changed for only two cases of 388 and 390.*/

	*br if conflict_id == 388 /*Russia or Azerbaijan in 1991? ->  Russia (because of missing data of control variables, military personnel and ODA, for Azerbaijan)*/
	replace location = "Russia (Soviet Union)" if conflict_id == 388 & year == 1991
	replace GWccode = 365 if conflict_id == 388 & year == 1991
	replace territory_name = "Nagorno-Karabakh" if conflict_id == 388 & year == 1991

	*br if conflict_id == 390 /*Yugoslavia or Croatia in 1992? -> Yugoslavia (to apply the same specification as 388 even though no missing data exist for Croatia-1992.*/
	replace location = "Serbia (Yugoslavia)" if conflict_id == 390 & year == 1992
	replace GWccode = 345 if conflict_id == 390 & year == 1992
	replace territory_name = "Croatia" if conflict_id == 390 & year == 1992

	/*432, 227, 13809, and 13306 can also be checked.*/
	replace territory_name = "Chechnya" if conflict_id == 432 & year == 2007
	replace territory_name = "Donetsk, Lugansk" if conflict_id == 13306 & year == 2014

	duplicates drop conflict_id year, force
	save "Data/01. Conflicts/UCDP_datasets_annual.dta", replace



********************************************************************************
********************************************************************************
********************************************************************************

/*Original UN peace operations dataset*/

*------------------------------------------------------------------------------*
/*(a) UN PKO presence                                                         */
*------------------------------------------------------------------------------*
	use "Data/02. Peacekeeping/UNPO2_pko_conflict-month.dta", clear
	sort conflict_id year month
	foreach var of varlist pko pko_robust {
		by conflict_id year: egen max_`var' = max(`var')
		replace `var' = max_`var'
		drop max_`var'
	}

	/*When more than one missions were deployed within a year*/
	gen x = .
	*tab conflict_id, m
	*tab year, m
	foreach num of numlist ///
		218 228 230 233 248 252 260 283 287 288 290 298 299 300 302 309 ///
		316 324 327 330 331 332 336 337 341 361 371 372 374 381 382 389 ///
		390 392 395 397 398 409 412 416 419 431 11343 11344 11345 11347 {
		foreach num2 of numlist 1948/2019 {
			quietly tab pko1 if conflict_id == `num' & year == `num2'
			scalar r1 = r(r)
			quietly tab pko2 if conflict_id == `num' & year == `num2'
			scalar r2 = r(r)
			quietly tab pko3 if conflict_id == `num' & year == `num2'
			scalar r3 = r(r)
			replace x = r1 + r2 + r3 if conflict_id == `num' & year == `num2'
		}
	}

	*br if x >= 2
	replace pko2 = "UNIPOM"     if conflict_id == 218 & (year == 1965 | year == 1966)
	replace pko2 = "MONUSCO"    if conflict_id == 283 & year == 2010
	replace pko1 = "UNAMIC"     if conflict_id == 300 & year == 1992
	replace pko2 = "UNTAC"      if conflict_id == 300 & year == 1992
	replace pko2 = "UNAMID"     if conflict_id == 309 & year == 2007
	replace pko1 = "UNMIS"      if conflict_id == 309 & year == 2011
	replace pko2 = "UNAMID"     if conflict_id == 309 & year == 2011
	replace pko1 = "UNAVEM I"   if conflict_id == 327 & year == 1991
	replace pko2 = "UNAVEM II"  if conflict_id == 327 & year == 1991
	replace pko1 = "UNAVEM II"  if conflict_id == 327 & year == 1995
	replace pko2 = "UNAVEM III" if conflict_id == 327 & year == 1995
	replace pko1 = "UNAVEM III" if conflict_id == 327 & year == 1997
	replace pko2 = "MONUA"      if conflict_id == 327 & year == 1997
	replace pko1 = "UNTAET"     if conflict_id == 330 & year == 2002
	replace pko2 = "UNMISET"    if conflict_id == 330 & year == 2002
	replace pko1 = "UNOSOM I"   if conflict_id == 337 & year == 1993
	replace pko2 = "UNOSOM II"  if conflict_id == 337 & year == 1993
	replace pko1 = "UNOMUR"     if conflict_id == 374 & (year == 1993 | year == 1994)
	replace pko2 = "UNAMIR"     if conflict_id == 374 & (year == 1993 | year == 1994)
	replace pko1 = "UNMIH"      if conflict_id == 381 & year == 1996
	replace pko2 = "UNSMIH"     if conflict_id == 381 & year == 1996
	replace pko1 = "UNSMIH"     if conflict_id == 381 & year == 1997
	replace pko2 = "UNTMIH"     if conflict_id == 381 & year == 1997
	replace pko3 = "MIPONUH"    if conflict_id == 381 & year == 1997
	replace pko1 = "MINUSTAH"   if conflict_id == 381 & year == 2017
	replace pko2 = "MINUJUSTH"  if conflict_id == 381 & year == 2017
	replace pko2 = "UNAMSIL"    if conflict_id == 382 & year == 1999
	replace pko2 = "UNMIBH"     if conflict_id == 389 & year == 1995
	replace pko2 = "UNCRO"      if conflict_id == 390 & year == 1995
	replace pko1 = "UNCRO"      if conflict_id == 390 & year == 1996
	replace pko2 = "UNTAES"     if conflict_id == 390 & year == 1996
	replace pko3 = "UNMOP"      if conflict_id == 390 & year == 1996
	replace pko1 = "UNTAES"     if conflict_id == 390 & year == 1998
	replace pko2 = "UNMOP"      if conflict_id == 390 & year == 1998
	replace pko3 = "UNPSG"      if conflict_id == 390 & year == 1998
	replace pko2 = "UNMIBH"     if conflict_id == 397 & year == 1995
	replace pko2 = "UNMIBH"     if conflict_id == 398 & year == 1995
	replace pko1 = "MINUCI"     if conflict_id == 419 & year == 2004
	replace pko2 = "UNOCI"      if conflict_id == 419 & year == 2004

	duplicates drop conflict_id year, force
	drop month ym x
	save "Data/02. Peacekeeping/UNPO2_pko_conflict-year.dta", replace


*------------------------------------------------------------------------------*
/*(b) UN SPM presence                                                         */
*------------------------------------------------------------------------------*
	use "Data/02. Peacekeeping/UNPO3_spm_conflict-month.dta", clear
	sort conflict_id year month

	/*When more than one missions were deployed within a year*/
	gen x = .
	tab conflict_id, m
	tab year, m
	foreach num of numlist ///
		233 259 260 269 271 287 289 316 327 330 333 ///
		337 341 369 381 382 395 410 416 11346 {
		foreach num2 of numlist 1993/2019 {
			quietly tab spm1 if conflict_id == `num' & year == `num2'
			scalar r1 = r(r)
			quietly tab spm2 if conflict_id == `num' & year == `num2'
			scalar r2 = r(r)
			replace x = r1 + r2 + r3 if conflict_id == `num' & year == `num2'
		}
	}

	*br if x >= 2
	replace spm1 = "UNMC"    if conflict_id == 289 & year == 2017
	replace spm2 = "UNVMC"   if conflict_id == 289 & year == 2017
	replace spm1 = "MINUSAL" if conflict_id == 316 & year == 1996
	replace spm2 = "ONUV"    if conflict_id == 316 & year == 1996
	replace spm1 = "UNOA"    if conflict_id == 327 & year == 2002
	replace spm2 = "UNMA"    if conflict_id == 327 & year == 2002
	replace spm1 = "UNPOS"   if conflict_id == 337 & year == 2013
	replace spm2 = "UNSOM"   if conflict_id == 337 & year == 2013
	replace spm1 = "MICIVIH" if conflict_id == 381 & year == 2000
	replace spm2 = "MICAH"   if conflict_id == 381 & year == 2000
	replace spm1 = "UNIOSIL" if conflict_id == 382 & year == 2008
	replace spm2 = "UNIPSIL" if conflict_id == 382 & year == 2008

	duplicates drop conflict_id year, force
	drop month ym x
	save "Data/02. Peacekeeping/UNPO3_spm_conflict-year.dta", replace


*------------------------------------------------------------------------------*
/*(c) Financial resources                                                     */
*------------------------------------------------------------------------------*
	use "Data/02. Peacekeeping/UNPO6_financial_resources_conflict-month.dta", clear
	sort conflict_id year month
	gen length_month = mdy(month+1, 1, year) - mdy(month, 1, year) if month != 12
	replace length_month = mdy(1, 1, year+1) - mdy(month, 1, year) if month == 12
	gen length_year = mdy(1, 1, year+1) - mdy(1, 1, year)

	*tab month if year == 2019 /*only unitl June; because the size of expenditures are all missing after July, I cannot calculate the average for 2019.*/
	drop if year == 2019

	foreach var of varlist militarybdg-nbd srf_militarybdg-ln_pop_nbd {
		replace `var' = `var' * length_month                             /*sum of daily average per month = monthly sum*/
		by conflict_id year: egen missing = sum(missing(`var'))
		by conflict_id year: egen sum_`var' = sum(`var') if missing == 0 /*sum of monthly sum per year = annually sum*/
		replace `var' = sum_`var' / length_year                          /*annually sum divided by # of days per year = daily average (in a given year)*/
		drop missing sum_`var'
	}

	by conflict_id year: gen num_month_fresources = _N
	duplicates drop conflict_id year, force
	drop ym month
	label variable num_month_fresources "# of months with UN PKO financial resources"
	save "Data/02. Peacekeeping/UNPO6_financial_resources_conflict-year.dta", replace


*------------------------------------------------------------------------------*
/*(d) Personnel                                                               */
*------------------------------------------------------------------------------*
	use "Data/02. Peacekeeping/UNPO7_personnel_conflict-month.dta", clear
	sort conflict_id year month

	*tab month if year == 2019 & military != . /*only unitl September; because the size of expenditures are all missing after October, I cannot calculate the average for 2019.*/
	drop if year == 2019

	foreach var of varlist military-officernat srf_military-ln_pop_officernat {
		by conflict_id year: egen missing = sum(missing(`var'))
		by conflict_id year: egen sum_`var' = sum(`var') if missing == 0
		replace `var' = sum_`var' / 12 /*per month*/
		drop missing sum_`var'
	}

	by conflict_id year: gen num_month_personnel = _N
	duplicates drop conflict_id year, force
	drop ym month
	label variable num_month_personnel "# of months with UN PKO personnel"
	save "Data/02. Peacekeeping/UNPO7_personnel_conflict-year.dta", replace


*------------------------------------------------------------------------------*
/*(e) Merge!                                                                  */
*------------------------------------------------------------------------------*
	use "Data/02. Peacekeeping/UNPO2_pko_conflict-year.dta", clear

	/*Financial resources*/
	merge 1:1 conflict_id year using "Data/02. Peacekeeping/UNPO6_financial_resources_conflict-year.dta"
	*tab year if _merge == 1        /*No financial resources data for 2019.*/
	*tab conflict_id if _merge == 2 /*only financial resources data exist for these 15 obs*/
	recode pko pko_robust (.=0) if _merge == 2
	rename _merge _merge_f

	/*Personnel*/
	merge 1:1 conflict_id year using "Data/02. Peacekeeping/UNPO7_personnel_conflict-year.dta"
	*br if _merge == 1 & year != 2019 /*mostly _merge_f == 2, that is, only financial resources data exist (PKO presence data did not exist)*/
	rename _merge _merge_p
	*tab _merge_f _merge_p
	drop _merge*

	save "Data/02. Peacekeeping/UNPO_datasets_annual.dta", replace



********************************************************************************
********************************************************************************
********************************************************************************

/*Merge all datasets*/

	use "Data/01. Conflicts/UCDP_datasets_annual.dta", clear

/*(a) UN PKOs*/
	merge m:1 conflict_id year using "Data/02. Peacekeeping/UNPO_datasets_annual.dta"
	*tab _merge                                /*2: 342 obs / 3: 328 obs*/
	*tab _merge if year >= 1989 & year <= 2018 /*2: 171 obs / 3: 328 obs*/
	*tab pko1 if _merge == 2 & year >= 1989 & year <= 2018 /*Mostly same as for the monthly data.*/
	foreach var of varlist pko pko_robust militarybdg-num_month_personnel {
		recode `var' (.=0) if _merge == 1
	}
	drop if _merge == 2
	drop _merge


/*(b) UN SPMs*/
	merge m:1 conflict_id year using "Data/02. Peacekeeping/UNPO3_spm_conflict-year.dta"
	*tab _merge                                /*2: 12 obs / 3: 216 obs*/
	*tab _merge if year >= 1989 & year <= 2018 /*2:  3 obs / 3: 328 obs*/
	*tab spm1 if _merge == 2 & year >= 1989 & year <= 2018 /*Mostly same as for the monthly data.*/
	recode spm (.=0) if _merge == 1
	recode spm_robust (.=0) if _merge == 1
	drop if _merge == 2
	drop _merge


/*(c) Other controls*/
	/*World Bank data*/
	merge m:1 GWccode year using "Data/03. Controls/Ctrl2_World_Bank_Open_Data.dta"
	*tab location year if _merge == 1 /*South Yemen in 1989, 1990*/
	drop if _merge == 2
	drop _merge

	foreach var of varlist srf-pop {
		gen ln_`var' = ln(`var')
	}

	/*V-Dem data*/
	merge m:1 GWccode year using "Data/03. Controls/Ctrl3_V-Dem.dta"
	/*_merge == 1: 0 obs*/
	drop if _merge == 2
	drop _merge

	/*MILINDA*/
	merge m:1 conflict_id year using "Data/03. Controls/Ctrl4_MILINDA.dta"
	*tab conflict_id if _merge == 2 & year >= 1989 /*Mostly same as for the annual data*/
	foreach var of varlist ropko-num_stpko {
		recode `var' (.=0) if _merge == 1
	}
	drop if _merge == 2
	drop _merge


/*(d) Other variables for analysis*/
	gen time = year - year(start_date) + 1

	sort conflict_id year
	xtset conflict_id year
	label data "Replication data for Does Peacekeeping by Civilians Work? (annual data)"
	save "Data/Dataset_annual.dta", replace





